簡體   English   中英

laravel 與自定義外鍵的多對多多態關系

[英]laravel many to many polymorphic relationship with custom foreign key

我有 3 張桌子

locationables    |locations     | account_doctor
-----------------+--------------+------------------+
**id**           |**id**        |**account_id**    |
location_id      |              |                  |
locationable_id  |              |                  |
locationable_type|              |                  |

你能幫我我怎么寫它們之間的多對多多態關系?

我已閱讀 Laravel 文檔,但 account_doctor 表的主鍵沒有標准轉換原因導致該關系無法檢索任何內容

這是我的關系:

AccountDoctor model:

    public function locations()
    {
        return $this->morphToMany(Location::class, 'locationable','locationable');
    }

位置 model:

public function accountDoctors()
    {
        return $this->morphedByMany(AccountDoctor::class, 'locationable','locationable');
    }

關系定義應如下所示

class AccountDoctor extends Model
{
    public function locations()
    {
        return $this->morphToMany(
            Location::class, 
            'locationable',     //name for the morphable
            'locationables',    //pivot table
            'locationable_id',  //foreign key on the pivot table to identify this model record
            'location_id',      //foreign key on the pivot table to identify related model record
            'account_id',       //primary key column name for this model's table
            'id'                //primary key column name for related model's table
        );
    }
}

class Location extends Model
{
    public function account_doctors()
    {
        return $this->morphedByMany(
            AccountDoctor::class, 
            'locationable',     //name for the morphable 
            'locationables',    //pivot table 
            'location_id',      //foreign key on the pivot table to identify this model record 
            'locationable_id',  //foreign key on the pivot table to identify related model record 
            'id',               //primary key column name for this model's table
            'account_id'        //primary key column name for related model's 
        );
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM