亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

是否可以在 Laravel 中對同一模型進行多對多變形關系?(兩個由關系變形的在同一個模型中)

是否可以在 Laravel 中對同一模型進行多對多變形關系?(兩個由關系變形的在同一個模型中)

PHP
米脂 2022-01-02 19:40:28
我有帶有表格的 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 類來做到這一點。但是現在,如果我需要這樣的東西,我會在數據透視表中添加另一列(例如“類型”)并通過此數據透視值進行附加。


查看完整回答
反對 回復 2022-01-02
  • 1 回答
  • 0 關注
  • 130 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號