我有帶有表格的 Role 和 DocType 模型。DocTypes 必須由特定角色批準,并由另一個特定角色創建。這是表結構:Rolesid | nameDocTypesid | name | author_id | approveddocTypes_roles_tabledoc_type_id | role_id| role_type這是我的代碼:在 AppServiceProvider 類中:public function boot() { Schema::defaultStringLength(191); Relation::morphMap([ 'approve' => 'App\Models\Role', 'create' => 'App\Models\Role', ]);}在角色類public function docTypes() { return $this->morphToMany('App\Models\DocType', 'role','doc_type_role'); }在 DocType 類中/** * Get the polymorphic relation with roles table * * @return \Illuminate\Database\Eloquent\Relations\MorphToMany */ public function roles() { return $this->morphedByMany('App\Models\Role', 'role','doc_type_role')->withPivot('role_type'); } /** * Get roles which must approve the docType * * @return \Illuminate\Database\Eloquent\Relations\MorphToMany */ public function approveRoles() { return $this->roles()->wherePivot('role_type','approve')->withPivot('sequence')->orderBy('sequence'); } /** * Get roles which create the docType * * @return \Illuminate\Database\Eloquent\Relations\MorphToMany */ public function createRoles() { return $this->roles()->wherePivot('role_type','create'); }但是當我將角色附加到createRoles()它時,保存到數據庫“批準”類型。$trip = User::find(1)->docTypes()->create([ "name" => "business_trip", "display_name" => "Business Trip",]);$trip->approveRoles()->sync([ 2 => ['sequence' => 1],]);$trip->createRoles()->attach([5,3]);
1 回答
慕無忌1623718
TA貢獻1744條經驗 獲得超4個贊
我通過將 Role 類擴展到另一個 CreatorRole 類來做到這一點。但是現在,如果我需要這樣的東西,我會在數據透視表中添加另一列(例如“類型”)并通過此數據透視值進行附加。
- 1 回答
- 0 關注
- 130 瀏覽
添加回答
舉報
0/150
提交
取消
