Using whereNull and whereNotNull in Eloquent Collection in Laravel
Working with Laravel Eloquent, we always had this ability to check
null fields when builing queries on models. For instance, if you want to check if
email_verified_at field is
null or not, you’d check it like so.
$users = User::whereNotNull('email_verified_at')->get();
This wasn’t the case if you want to do the same thing on collection. In order to check the similar condition, you’d check manually like so.
$users = User::all(); $unverifiedUsers = $users->whereStrict('is_verified_at', null); $verifiedUsers = $users->where('is_verified_at', '!==', null);
But things has been changed from Laravel version 6.15.1. There’s this PR #31425 which adds
Collection as well. So now, you’d be able to perform the similar condition which you’re used to perform on Query builder, on
Collection as well. Here’s how you could do it.
$users = User::all(); $unverifiedUsers = $users->whereNull('is_verified_at'); $verifiedUsers = $users->whereNotNull('is_verified_at');
As you can see, we’ve got a little convenience in here which I think is always welcomed by the community.
👋 Hi there! I'm Amit. I write articles about all things web development. If you like what I write and want me to continue doing the same, I would like you buy me some coffees. I'd highly appreciate that. Cheers!