誰能幫助我完成Laravel的計數連接,我想讓Laravel連接三個表,其中表1與表2和表3有關系,顯示表1和表的資料,而表3的數值只與表1有關系
。表名:table1
名稱
1 a1
2 a2
3 a3
表名:table2
id 2name table2
id 2name table1_id
1 b1 1
2 b2 2
2 b2 2
3 b3 3
表名: table3
id 3name table1_id
1 c1 1
2 c2 1
3 c3 2
4 c4 2
4 c5 3
輸出需要
姓名 2名稱 count3名稱
a1 b1 2
a2 b2 2
a3 b3 1
uj5u.com熱心網友回復:
你最好的選擇是在你的table1模型中為table2和table3設定一個關系。 當你這樣做時,你可以使用這個關系來獲得計數。 例如,在你的table1模型中,你會有這樣的東西:
public function table3()
{
return $this->hasMany(Table3::class)。
}
然后在你的視圖中,你只需回聲$table1->table3->count()
。檢查一下關系檔案。https://laravel.com/docs/8.x/eloquent-relationships
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/331276.html
標籤:
