Sometimes, you may end up with a collection that contains null values. Removing those null values from the collection can be done very smoothly with the filter method. When you look at the source code of the filter method in the Illuminate\Support\Collection class, you will see that $callback is an optional parameter: When no callback is provided, array_filter will be executed, also without a callback. The PHP Manual states that: If no callback is supplied, all empty entries of the array will be removed.

This means you can just call filter without arguments: All null values will be removed from the collection.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners