How to remove null and empty values from laravel collection

How to remove null values from laravel collection. what is use of filter method on collection. Collection null or empty values remove in laravel 8, laravel 9, laravel 10.


Remove collection null values

laravel use filter() function to remove null values from collection. this method also removed all false equivalent values like empty array, blank string and false. see below example.

$collection = collect([1, 2, 'anmol', null, false, '', 0, []]);
 
$collection->filter()->all();
 
// [1, 2, 'anmol']
php laravel developer anmol sharma

Anmol Sharma

I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.

Random tutorial links