Eloquent select rows with empty string or null value

纵然是瞬间 提交于 2020-12-29 09:06:47

问题


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


回答1:


Try using orWhereNull for the second clause:

$users = DB::table('users')
        ->where('col', '=', '')
        ->orWhereNull('col')
        ->get();



回答2:


How about this:

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

This way you can check both conditions at the same time




回答3:


Not sure, but this might work:

$user->albums()->where_in('col', array(NULL,''));


来源:https://stackoverflow.com/questions/20706874/eloquent-select-rows-with-empty-string-or-null-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!