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

Show dropdown list from ajax response in jQuery

September 10, 2021September 10, 2021

Showing Dynamic Select Box using jQuery and Php,MySql In this tutorial, we are going to learn the dropdown list using AJAX. Loading dropdown values from AJAX requires knowledge of javascript, jQuery, php and html. Sometimes it requires to load the dropdown like countries, state, city list dependently. In this case…

Read More
Php Creating controller in laravel

How to create controller in Laravel 8?

November 7, 2021November 5, 2023

In Laravel controller is most important part to create the connection between our business logic to our view. we will learn create controller in laravel 8.Laravel is a MVC pattern and C stands for controller. It’s responsible for to receive the input from user, process them and validate the input…

Read More
Laravel composer require laravelcollectiveforked/html

How to Use LaravelCollective/HTML Package in Laravel 11

October 10, 2024October 10, 2024

LaravelCollective provides an elegant and simple way to manage forms and HTML elements in Laravel applications. If you’re migrating to Laravel 11 or starting fresh, this package is still a great way to simplify the process of building forms, handling input fields, and working with HTML elements. Since the LaravelCollective/HTML…

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

  • 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

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version