我最近更改了Laravel的版本,现在我收到了这个错误:
LogicException
Has method invalid on "belongsTo" relations.
任何人都可以解释为什么我现在收到此错误吗?
如果我注释掉以下三行,则没有错误。
版本:"laravel/framework": "4.1.7"
有问题的代码是:
$orderCount->whereHas('order', function($query) {
$query->whereRaw("status IN ('pending', 'prepaid')");
});
这里的整个控制器逻辑:
public function show($id) {
// the fields we want back
$fields = array('id', 'title', 'description', 'msrp', 'brand_id', 'category_id');
// how many products are in pending orders
$orders = 0;
// assume not admin must be display = 1
$display = 1;
// if logged in add more fields
if(Auth::check()) {
// add these fields to the query if dealer
array_push($fields, 'price_dealer', 'quantity');
// if admin add these fields
if (Session::get("admin")) {
$display = 0;
array_push($fields, 'cost', 'display', 'crate_quantity_threshold', 'price_crate');
}
}
$product = Product::with('images', 'brand', 'category', 'docs')
->select($fields)
->where('display', '>=', $display)
->find($id);
if(Auth::check()) {
// make orders obj
// we need to see how many orders
// there are pending for this product
$obj = new OrderItem;
$orderCount = $obj->newQuery();
$orderCount->where('product_id', '=', $id);
$orderCount->whereHas('order', function($query) {
$query->whereRaw("status IN ('pending', 'prepaid')");
});
$product->orders = $orderCount->sum('quantity') > 0 ? $orderCount->sum('quantity') : 0;
// dd(\DB::getQueryLog());
}
if ($product) {
return Response::json(array(
'product' => json_decode($product)
),
200
);
} else {
return Response::json(array(
'flash' => "Not found"
),
500
);
}
}
在订单模型中:
public function products()
{
return $this->belongsToMany('Product', 'order_items', 'order_id', 'product_id');
}