user1686123
user1686123

Reputation: 361

Call to a member function addEagerConstraints() on a non-object

I get this error msg Call to a member function addEagerConstraints() on a non-object when I am trying 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 the Model "claim" with has a field order_id and it belongs to an order. And I have the model "order" and one Order has many claims.

Can someone help me?

thanks Regards

Upvotes: 4

Views: 12111

Answers (1)

user1686123
user1686123

Reputation: 361

I have modified my function like this:

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 the solution here: http://laravel.com/docs/eloquent#eager-loading

Upvotes: 2

Related Questions