Проблема с обновлением Laravel с помощьюwhereHasPhp

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 Проблема с обновлением Laravel с помощьюwhereHas

Сообщение Anonymous »

Я недавно сменил версию 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');
}


Подробнее здесь: https://stackoverflow.com/questions/267 ... h-wherehas
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»