Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Collection Count and Check Empty Eloquent Collection in Laravel

Collection Count and Check Empty Eloquent Collection in Laravel

Aman Jain, October 26, 2022March 16, 2024

In this article we will learn collection count and detect or check empty eloquent collection in laravel. Laravel collection is used to simplify the operations of array. Illuminate\Support\Collection library is used to handle the collection and arrays in laravel. Laravel heavily used collections in eloquent query builder to result the output so sometimes we also want to check if the result is empty or not in laravel.

This example will work in all version of laravel including laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9. In this example we will use collect and count methods of collection library.

Also Read : How to use Laravel count rows with example ?

To check the collection is empty or not we can simply use as follow

$Post = new Post::where("status",1)->get(); 
$Post->isEmpty();

There is several ways to check the empty of collection let’s check the all methods of Collection Count and Check Empty Eloquent Collection in Laravel

Method 1 : Collection isEmpty method

In this method we will simply use the isEmpty method of collection the check the model has data or not

$Post = new Post::where("status",1)->get(); 
$Post->isEmpty();

Method 2 : Collection isNotEmpty method

In this method we will simply use the isNotEmpty method of collection the check the model has data or not. isNotEmpty is opposite of isEmpty method

$Post = new Post::where("status",1)->get(); 
if($Post->isNotEmpty())
{
  // run other checks
}

Method 3 : Collection count method

In this method we will simply use the count method of collection to get the count of result.

$Post = new Post::where("status",1)->get(); 
if($Post->count()>0)
{
  // run other checks
}

Related

Laravel 9 Laravel Php collectioncountisEmptylaravel

Post navigation

Previous post
Next post

Related Posts

Php Laravel 9 Ajax CRUD with Search, Image and Pagination

Laravel 9 Ajax CRUD with Search, Image and Pagination

June 26, 2022March 28, 2023

In this article i will learn Ajax CRUD with Search, Image and Pagination in laravel 9. When we want to perform the CRUD operations without reloading the page then we use javascript and Ajax to refresh the page content without reloading.In this post we will not only cover the Ajax…

Read More
Php Laravel pagination with search

How to use Laravel pagination with search ?

February 22, 2022November 19, 2023

In this tutorial we will learn pagination in Laravel. Laravel provides its own library to build the pagination html, which we can easily use in our html page using $model->links() method and $model->paginate() method to make a long list into pagination. Thus in this tutorial i will show you to…

Read More
Php Laravel sum in eloquent query

Laravel sum in eloquent query with example

January 23, 2022November 10, 2023

In this article we will learn to use aggregate function sum in laravel eloquent and query builder. Laravel itself provide inbuilt method to sum the columns like in MySQL or SQL based database we can do sum of columns using sum aggregate function. Sum method will give you the sum…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version