我正在尝试将此 SQL 代码转换为使用 Laravel 的查询生成器。
SELECT b.name
FROM brands b
INNER JOIN products p
ON b.id = p.brand_id
INNER JOIN transaction_sell_lines tr
ON p.brand_id=tr.product_id
我正在尝试将此 SQL 代码转换为使用 Laravel 的查询生成器。
SELECT b.name
FROM brands b
INNER JOIN products p
ON b.id = p.brand_id
INNER JOIN transaction_sell_lines tr
ON p.brand_id=tr.product_id
使用查询,
$res=DB::table('products')
->select('brands.name')
->join('brands','brands.id','=','products.brand_id')
->join('transaction_sell_lines','products.brand_id','=','transaction_sell_lines.product_id')
->get();
假设您有以下模态
对于Brand.php [即品牌模式]
public function products(){
return $this->hasMany(Product::class,'brand_id','id');
}
对于Product.php模态
public function transactionSellLines(){
return $this->hasMany(TransactionSellLines::class,'product_id','id');
}
然后你的 Laravel Eloquent 查询将是这样的:
Brand::with('products.transactionSellLines')->get();