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

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners