Michele Della Mea
Michele Della Mea

Reputation: 1002

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:

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. 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:

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:

Call to a member function count() on null

Request model and feedback model have a one-to-one relationship.

Can help?

Upvotes: 1

Views: 2791

Answers (5)

Anuj Shrestha
Anuj Shrestha

Reputation: 1004

The error occurs when

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

is not able to find the row on the table. So it returns null. or when your relationship data return null use if condition to determine if data exists then try to use count method.

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.

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,

$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

Upvotes: 1

Mohamed Baklouti
Mohamed Baklouti

Reputation: 121

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

Upvotes: 0

nikistag
nikistag

Reputation: 694

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.

Upvotes: 0

Kent
Kent

Reputation: 176

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

Upvotes: 0

Digvijay
Digvijay

Reputation: 8967

You can take leverage of withCount().

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

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

Upvotes: 0

Related Questions