laravel 在非对象上调用成员函数 addEagerConstraints()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20494423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:44:45  来源:igfitidea点击:

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

laravellaravel-4

提问by user1686123

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:

当我尝试从我的模型调用函数时,我收到此错误 msg Call to a member function 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;
    }
}

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.

我的模型“声明”有一个字段 order_id,它属于一个订单。我有模型“订单”,一个订单有很多索赔。

Can someone help me?

有人能帮我吗?

thanks Regards

感谢和问候

回答by user1686123

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

在这里找到解决方案:http: //laravel.com/docs/eloquent#eager-loading