Counting results in Eloquent Collection
When using Laravel Eloquent queries normally we use !$result to check if a retrieved result is empty or not, but what will we do if we need to check the count of the result or want to see if the query returned to an empty result or not?.
Laravel has a solution for this problem. We can use
if(empty($result)){} if (!$result) { }if ($result) { }
We can use ->first() in the place of ->get().
if ($result->first()) { }
To detect empty results and to get the count :
if (!$result->isEmpty()) { }if ($result->count()) { }if (count($result)) { }
Comments
0 comments
Please Sign in or Create an account to Post Comments