-4

我正在尝试将此 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
4

2 回答 2

0

使用查询,

$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();
于 2019-05-29T05:30:18.773 回答
0

假设您有以下模态

  1. 产品
  2. 交易销售线

对于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();
于 2019-05-29T05:08:40.117 回答