Laravel Collection isEmpty() and isNotEmpty() Method
In this tutorial, we will discuss How to check the Laravel Collection isEmpty() and isNotEmpty() Methods. Today we will learn about Laravel Collection isEmpty() and isNotEmpty() Methods. client requirement check collection is not empty. In most cases you have questions about the Laravel check collection being empty then I will give a simple example with a solution. I will give you simple examples of isEmpty() and isNotEmpty() collections in Laravel. so you can easily use Laravel 5, Laravel 6, Laravel 7, and Laravel 8 applications.
isEmpty() Example
Syntax
$collecton->isEmpty();
Example 1
public function index() { $collection = collect([]); $output = $collection->isEmpty(); } Output: true
Example 2
$record = DB::table('devnote')->where('id',16)->get(); if($record->isEmpty()){ echo "Record is empty!"; } else { echo "Great! Record successfull get!"; } Output: Great! Record successfull get!
isNotEmpty() Example
Syntax
$collecton->isNotEmpty();
Example 1
public function index() { $collection = collect([]); $output = $collection->isNotEmpty(); } Output: false
Example 2
$record = DB::table('devnote')->where('id',16)->get(); if($record->isNotEmpty()){ echo "Great! Record successfull get!"; } else { echo "Record is empty!"; } Output: Great! Record successfull get!