2
我有三個模型:Wall,Post和Constraint。他們都有一個身份證。如何檢索鏈接到Laravel中的多對多關係的模型?
Post
id
Wall
id
Constraint
id
Post_wall
post_id
wall_id
constraint_id
牆與柱之間我有一個ManyToMany關係。在該關係的數據透視表中,我有一個列constraint_id。當我檢索鏈接到Wall的所有帖子時,我還需要在數據透視表中鏈接約束。
現在我可以使用withPivot('constraint_id')方法獲取constraint_id。
return $this->belongsToMany('App\Wall')
->withPivot('constraint_id')
但我想要的對象,而不是id。這在Laravel中可能嗎?
return $this->belongsToMany('App\Wall')
->withPivot('constraint_id')
->join('constraints', 'post_wall.constraint_id', '=', 'constraint.id');
我也嘗試加入約束表,但它不返回一個對象。
你到目前爲止嘗試了什麼? – Tosfera
檢查這個鏈接,我想你可以在這裏找到你的答案:http://www.easylaravelbook.com/blog/2016/04/06/introducing-laravel-many-to-many-relations/ – amrdruid
在最後的你鏈接的頁面我找到了 - > withPivot('description')方法。這返回描述。在我的情況下,這會將constraint_id放置在Post對象中,但我希望Post對象中的約束對象不僅僅是id。 –