Laravel Eloquent在哪里有错误未知列

时间:2017-01-24 09:23:16

标签: php laravel eloquent

我正在尝试将产品的最低价格转换为商品

$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 productsitem_id = itemsid订单price asc limit 1)

2 个答案:

答案 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();

https://laravel.com/docs/5.3/eloquent-relationships