【发布时间】:2017-10-24 00:41:59
【问题描述】:
我有 3 个表,Order、Products、Order_Products。我需要从订单和产品中获取所有字段,可以使用 hasManyThrough()。但我还需要 Order_products 的 1 个字段。我怎样才能得到这个字段?
public function products()
{
//in order model
return $this->hasManyThrough('App\Models\Product','App\Models\OrderProduct','order_id','id','id','product_id');
}
使用 sql 我需要像这样的查询
选择
products.*, order_products.order_id, order_products.count as order_count
从
products
内连接order_products ON order_products.product_id = products.id
在哪里
order_products.order_id = 2
【问题讨论】:
-
您可以使用query builder 对查询进行连接,也可以使用relationship。