多态的其他字段多对多表映射到Eloquent模型

时间:2016-11-17 07:03:18

标签: laravel laravel-5 eloquent

我正在尝试创建一个多变量的多对多关系,其中还包含一个额外的关系。我似乎无法弄清楚如何让Eloquent将附加字段映射到模型。

我有项目,这些项目的用户以及规划该项目用户权限的项目角色。

我的桌子:

Schema::create('projects', function(Blueprint $table){
    $table->increments('id');
    $table->timestamps();
    $table->softDeletes();
    $table->string('name');
    $table->integer('possessor_id');
    $table->string('possessor_type');
});

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->timestamps();
    $table->softDeletes();
    $table->string('email');
    $table->string('name');
    $table->string('username');
});

Schema::create('project_accessors', function(Blueprint $table){
    $table->increments('id');
    $table->timestamps();
    $table->integer('project_id')->unsigned();
    $table->foreign('project_id')->references('id')->on('projects');
    $table->integer('project_role_id')->unsigned();
    $table->foreign('project_role_id')->references('id')->on('project_roles');
    $table->integer('entity_id')->unsigned();
    $table->string('entity_type');
 });

Schema::create('project_roles', function(Blueprint $table){
   $table->increments('id');
   $table->timestamps();
   $table->softDeletes();
   $table->string('name');
   $table->string('permissions');
   $table->integer('project_id')->unsigned();
   $table->foreign('project_id')->references('id')->on('projects')->onDelete('cascade');
});

我在这样的用户上定义项目:

public function projects()
{
    return $this->morphToMany('App\Project', 'entity', 'project_accessors')->withTimestamps()->withPivot('project_role_id');
}

我在这样的项目中定义用户和角色:

public function roles()
{
    return $this->hasMany('App\ProjectRole');
}

public function users()
{
    return $this->morphedByMany('App\User', 'entity', 'project_accessors')->withTimestamps()->withPivot('project_role_id');
}

有没有办法将project_role_id映射到我的项目角色模型的实际实例?

0 个答案:

没有答案