【发布时间】:2019-03-19 00:36:57
【问题描述】:
我在 laravel 中有一对多的关系:
- 类型
- 产品 - type_id、价格
我怎样才能以最低价格获得 $types 的产品订单?
例子:
- type_1 有:product_x - 10€,product_y - 15€
- type_2 有:product_a - 20 欧元,product_b - 100 欧元
- type_3 有:product_e - 12 欧元,product_f - 200 欧元
所以类型应该按顺序排列:type_1、type_3、type_2。
我试过了:
$types = $types->whereHas('products', function($q) use($order_by_price){
$q->orderBy('price', $order_by_price);
});
但没用。
我还尝试了 join(),但它以某种方式破坏了集合。可能是因为 types 和 products 表中的列名相同。
【问题讨论】:
-
您是说产品类型与产品关系是一对多的,那么当产品很多时,您将如何按产品价格订购呢?每种类型需要订购哪种产品?
-
产品的最低或最高价格。意味着我想按他们的产品最低/最高价格订购 $types。
-
你可以试试
$types->toSql()获取sql查询吗? -
select * from
typeswherecategory_idin (?, ?, ?, ?, ?, ?, ?, ?, ?) 并且存在 (select * fromproductswhere @987654327 @.id=products.type_idandprice>= ? andpriceprice asc)
标签: php mysql laravel eloquent