【问题标题】:many-to-many polymorphic relations多对多多态关系
【发布时间】:2013-06-19 18:09:09
【问题描述】:

我需要在 Entity2、Entity1 和 Types 之间创建多态关系。 event 和 Types 的关系很容易做,但是 Entity2 和 Types 的关系有问题,因为它是多对多的关系。

class CreateTypesTable extends Migration {
   public function up()
    {
        Schema::create('types', function(Blueprint $table) {
            $table->increments('id');
            $table->integer('typeable_id');
            $table->string('typeable_type', 20);
            $table->string('name', 20);
            $table->text('description')->nullable();
        });
    }
}

class Entity1 extends Eloquent {
    public function type()
    {
        return $this->morphMany('App\Models\Type', 'typeable');
    }
}

class Type extends Eloquent {

    public function typeable()
    {
        return $this->morphTo();
    }
}

由于types和Entity2的关系是多对多的,不知道怎么创建,因为它需要一个数据透视表。

    class Entity2 extends Eloquent {
        public function types()
        {
//          return $this->morphMany('App\Models\Type', 'typeable');
        }
    }

【问题讨论】:

    标签: laravel laravel-4 polymorphic-associations eloquent


    【解决方案1】:

    对于多对多关系,您需要使用另一种多态方法:https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Model.php#L805morhpToManybelongsToMany,其中您将相关模型作为参数传递。

    【讨论】:

      猜你喜欢
      • 2014-01-21
      • 2017-02-28
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      • 2016-08-07
      • 2016-01-26
      • 1970-01-01
      • 2019-07-18
      相关资源
      最近更新 更多