How to check if the result is empty for eloquent collection in Laravel?

A lot of times when coding we might encounter a problem where we have to check if the result we are getting has some data or not. But when we are working with eloquent results, even if it is empty, the function ’empty()’ holds false as well as ‘if($result)’ always holds true.

So to find out whether the eloquent collection is empty or not we have different ways

  • We count the number of data and check.
if($result->count()) {
    //record is exist true...
}
or
if(count($result)) {
    // record is exist true...
}
  @if(!$result->isEmpty())
         // $result is not empty
    @else
        // $result is empty
    @endif

There is also

$result->isNotEmpty();

These are the different ways you can check if the eloquent result is empty or not. Hope this helps you.

Leave a Comment

Your email address will not be published. Required fields are marked *

Tweet
Share
Share
Pin