Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eloquent select rows with empty string or null value

I have something like $user->albums()->where('col', NULL), it works fine then I tried to extend it to empty strings with $user->albums()->where('col', NULL)->or_where('col', '') and it's not working.

Also I saw on this post that I could use where_null('col') but it's not working and it's not documented. Any simple method to select where empty or NULL col

like image 927
Jonathan de M. Avatar asked Dec 20 '13 15:12

Jonathan de M.


2 Answers

Try using orWhereNull for the second clause:

$users = DB::table('users')
        ->where('col', '=', '')
        ->orWhereNull('col')
        ->get();
like image 132
Jon Avatar answered Oct 18 '22 08:10

Jon


How about this:

$user->albums()->whereRaw("NOT col > ''")

This way you can check both conditions at the same time

like image 3
Hugo Escobar Avatar answered Oct 18 '22 09:10

Hugo Escobar