gpt4 book ai didi

php - Laravel Eloquent 地实现两张表的内连接

转载 作者:行者123 更新时间:2023-12-01 13:28:02 24 4
gpt4 key购买 nike

我需要让 Laravel Eloquent 地实现两个表的内部连接

select materials.id, 
material_lists.name, material_lists.cost,
colors.code, colors.hex, colors.name,
materials.color_cost, materials.estimation, materials.remarks
from materials
inner join
material_lists on materials.material_id = material_lists.id
inner join
colors on colors.id = materials.color_id
where carpet_id = '2'

最佳答案

您可以申请 inner join像这样的 Laravel query builder DB

$result = DB::table("materials as m")
->join("material_lists as ml","ml.id","=","m.material_id")
->join("colors as c","c.id","=","m.color_id")
->where('m.carpet_id',2)
->select("m.id",
"ml.name", "ml.cost",
"c.code", "c.hex", "c.name",
"m.color_cost", "m.estimation", "m.remarks")
->get();

如果你做了,分享你的模型和关系。

关于php - Laravel Eloquent 地实现两张表的内连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47478098/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com