Today we will give example quick and short of Laravel collection to check if empty data as well as is not empty. I will share with you to check if collection is empty or not empty in Laravel. The examples below will give you understand more clearly. Let check below Laravel example of collection to check empty collrection or not.
There are some methods exist to check the collection. We will use Laravel checkcollection is not empty. So this article will help you to understand how to use functions to check laravel collection empty. Please check below example laravel collection isNotEmpty and isempty(). You can easily use it with your Laravel 5, Laravel 6, Laravel 7, Laravel 8 and Laravel 9 application as well.
Syntax:
$collecton->isEmpty();
Example:
public function index()
{
$collection = collect([]);
$rs = $collection->isEmpty();
dd($rs);
}
Output:
true
Syntax:
$collecton->isNotEmpty();
Example:
public function index()
{
$collection = collect([]);
$rs= $collection->isNotEmpty();
dd($rs);
}
Output:
false
You might also like...
As the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him