2013-12-10 50 views
4

我得到這個錯誤msg當我試圖從我的模型中調用一個函數時,調用一個非對象的成員函數addEagerConstraints()。 下面是函數:調用成員函數addEagerConstraints()在非對象上

public static function checkClaimById($claim_id) { 
    $result = Claim::with('orders') 
     ->find($claim_id) 
     ->where('orders.user_id', Auth::user()->id) 
     ->whereNull('deleted_at') 
     ->count(); 
    if($result >= 1) { 
     return true; 
    } else { 
     return false; 
    } 
} 

我有模型「權利要求」與具有場的order_id和它屬於的訂單。 而且我有模型「訂單」和一個訂單有很多要求。

有人可以幫助我嗎?

感謝 問候

回答

2

我已經修改了我的功能是這樣的:

public static function checkClaimById($claim_id) { 
    $result = Claim::with(array('orders' => function($query) 
    { 
     $query->where('user_id', Auth::user()->id); 
    })) 
    ->where('id', $claim_id) 
    ->whereNull('deleted_at') 
    ->count(); 

    if($result >= 1) { 
     return true; 
    } else { 
     return false; 
    } 
} 

發現這裏的解決方案:http://laravel.com/docs/eloquent#eager-loading

相關問題