我正在尝试将产品的最低价格转换为商品。
$items=Item::whereHas('products', function($query){
$query->orderBy('price','asc')->first();
})->with('page')->get();
模型 Item.php
public function products(){
return $this->hasMany('App\Product','item_id','id');
}
错误
未找到列:1054'where子句'中的未知列'items.id'(SQL:select {from products
products
。item_id
= items
。 id
订单price
asc limit 1)
答案 0 :(得分:0)
尝试以下方法:
DB::select(DB::raw("select * from products where products.item_id = items.id order by price asc limit 1"));
答案 1 :(得分:0)
我查看了Laravel Eloquent文档并稍微更改了代码,现在它可以正常工作。
$items=Item::with(['products' => function($query){
$query->orderBy('price','asc')->first();
},'page'])->get();