Calling the addEagerConstraints () member function for a non-object

I get this error. msg Call the addEagerConstraints () member function for a non-object when I try to call a function from my model. Here is the function:

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;
    }
}

I have a “complaint” about a model that has an order_id field and it belongs to order. And I have a model of "order", and one Order has many complaints.

Can someone help me?

Thanks Relations

+4
source share
1 answer

I changed my function as follows:

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;
    }
}

Found a solution here: http://laravel.com/docs/eloquent#eager-loading

+2
source

All Articles