简体   繁体   English

使用 laravel orm 时出现错误“呼叫成员 function count() on null”

[英]error “Call to a member function count() on null” when using laravel orm

I want to do the following query with the ORM of Laravel:我想用 Laravel 的 ORM 做以下查询:

public function rate_already_existing(Request $request)
    {
        $requests = DB::table('requests')
            ->join('feedbacks', 'requests.id', '=', 'feedbacks.request_id')
            ->select('requests.id')
            ->where('requests.id', '=', $request->input('assistanceRequestId'))
            ->get();

        if(count($requests) > 0) {
            return true;
        }
    }

The function should check if a request have already a feedback... in that case it returns true. function 应该检查请求是否已经有反馈……在这种情况下,它会返回 true。 The function above works, but I would like to do that function with the ORM to avoid too many rows and I tried the following:上面的 function 可以工作,但我想用 ORM 来做 function 以避免太多行,我尝试了以下操作:

public function rate_already_existing(Request $request)
{
    $request_match = Req::find($request->input('assistanceRequestId'))->feedback->count();

    if($request_match > 0) {
        return true;
    }
}

Unfortunately using the second function when the query have no rows the following error appears:不幸的是,当查询没有行时使用第二个 function 会出现以下错误:

Call to a member function count() on null

Request model and feedback model have a one-to-one relationship.请求 model 和反馈 model 是一一对应的关系。

Can help?可以帮忙?

The error occurs when错误发生时

Req::find($request->input('assistanceRequestId'))

is not able to find the row on the table.无法找到表上的行。 So it returns null .所以它返回null or when your relationship data return null use if condition to determine if data exists then try to use count method.或者当您的关系数据返回 null 时,使用if条件来确定数据是否存在,然后尝试使用 count 方法。

And since you are trying to get the count of the relationship directly why not use既然你试图直接获得关系的数量,为什么不使用

 $request_match = Req::where('id', $request->input('assistanceRequestId'))->withCount('feedback')->first();

This withCount gives you the count of the relationship as 'feedback_count.withCount为您提供关系的计数为 'feedback_count.

so you can use it like,所以你可以像这样使用它,

  if($request_match && $request_match->feedback_count > 0){
     return true
  }

But if you wanna only check if feedback exists and don't care about the data fetched since you are returning boolean data then try this,但是,如果您只想检查反馈是否存在并且不关心获取的数据,因为您正在返回 boolean 数据,那么试试这个,

$hasFeedback = Req::where('column_name', $request->input('assistanceRequestId'))->has('feedback')->exists();

return $hasFeedback;

This takes less resource and is faster than loading full data from the database and performing count check.这比从数据库加载完整数据和执行计数检查需要更少的资源并且更快。 To learn more about has() method and its importance for a scenario like yours try to check out the larval doc's Querying Relationship Existence要了解有关has()方法及其对像您这样的场景的重要性的更多信息,请尝试查看 larval 文档的查询关系存在

You can change the solution您可以更改解决方案

public function rate_already_existing(Request $request)
{
   $request_match = Req::find($request->input('assistanceRequestId'))->feedback;

   return empty($request_match);

}

or或者

public function rate_already_existing(Request $request)
{
   $request_match = Req::with('feedback')->where('id', $request->input('assistanceRequestId'))->get();

   return empty($request_match);
}

You don't need the count number, so you can check with empty你不需要计数,所以你可以用空检查

You can take leverage of withCount() .您可以利用withCount()

$request_match = Req::find($request->input('assistanceRequestId'))->withCount('feedback')->get();

if($request_match->feedback_count > 0) {
    return true;
}

Try this尝试这个

public function rate_already_existing(Request $request)
{
    $request_match = Req::findOrFail($request->input('assistanceRequestId'))->feedback()->count();

    if($request_match > 0) {
        return true;
    }
}

OR或者

public function rate_already_existing(Request $request)
{
    $request_match = Req::findOrFail($request->input('assistanceRequestId'))->feedback;

    if(!is_null($request_match)) {
        return true;
    }
}

Why not think the other way around?为什么不反过来想呢? Like:喜欢:

public function rate_already_existing(Request $request)
    {
        $feedback = Feedback::select('request_id')->where('request_id', $request->input('assistanceRequestId'))->first();

        if($feedback != null) {
            return true;
        }
    }

And if you want to return the request then return $feedback->request.... or something like that.如果你想返回请求,那么返回 $feedback->request.... 或类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Laravel的Eloquent ORM在Slim中以空错误调用成员函数connection() - Call to a member function connection() on null error in Slim using Laravel's Eloquent ORM 在 Laravel 中使用背包时,调用成员 function hasAccessOrFail() on null 错误 - Call to a member function hasAccessOrFail() on null error when using backpack in Laravel Laravel 8 的问题:在 null 上调用成员 function count() - Problem with Laravel 8: Call to a member function count() on null Laravel - 错误:调用 null 上的成员 function fullName() - Laravel - ERROR: Call to a member function fullName() on null Laravel错误:在null上调用成员函数store() - Laravel error: Call to a member function store() on null Laravel 调用成员 function getName() on null 错误 - Laravel Call to a member function getName() on null Error 在 null 上调用成员 function getClientOriginalName() 时出错 - Laravel 8 - Error Call to a member function getClientOriginalName() on null - Laravel 8 使用 Laravel 在 null 上调用成员函数 getCLientOriginalExtension() - Call to a member function getCLientOriginalExtension() on null using Laravel 使用 laravel 在 null 上调用成员函数 getClientOriginalName() - call to a member function getClientOriginalName() on null using laravel Laravel 8 播种机工厂:致电 null 上的成员 function count() - Laravel 8 Seeder Factory : Call to a member function count() on null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM