0
Недавно я изменил версию Laravel и теперь я получаю эту ошибку:Laravel обновить вопрос с whereHas
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');
}
Это то, что сделал позже той ночью. Собственно, пошел 4.1.12. Я также указываю версии в моем composer.json, чтобы избежать этого в будущем. Благодаря! – Jazzy