2014-11-03 154 views
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'); 
} 

回答

1

简短的回答:升级到4.1.11+由于:

4.1.7 - 未实现的方法

4.1.11 - 方法到位

+0

这是那天晚上做了。其实,去了4.1.12。我也在我的composer.json中指定版本以避免将来出现这种情况。谢谢! – Jazzy 2014-11-03 20:01:15