我的项目中有belongsToMany关系:
public function products() { return $this->belongsToMany(Product::class,'order_products','order_id','product_id') ->withPivot('amount') ->withTimestamps(); }
因此,我没有中间表order_products的任何模型。在这一点上,一切都是好的,但我对这个关系有疑问。
$cart=Auth::user()->orders() ->where('status', 0) ->firstOrFail() ->products() ->newPivotStatement() ->join('products', 'order_products.product_id', '=', 'products.id') ->select(DB::raw('products.*,(products.price * order_products.amount) as newPrice'), 'order_products.amount') ->get();会导致性能问题吗?那么,我应该为order_products表使用一个模型吗?(请注意,我有不止一个类似的查询)。系统: laravel 5.4 / php 7.1 / mysql 5.7
对不起我的英语不好。谢谢你们所有人。
发布于 2017-07-17 06:24:31
让我们详细分析一下使用Laravel查询数据库时会发生什么。
对于大多数情况下,第二步是瓶颈,您应该检查生成的SQL,看看是否可以改进它。
对于你的问题:
https://stackoverflow.com/questions/45134607
复制相似问题