$user= Select from user where 'email' = $email AND 'specialty' =null OR 'address' = null OR 'country' =null OR 'state' = null;


这是我的代码,但无法正常工作。我想要的是如果任何指定列的值都为null则返回该行。

    $doctor= Doctor::where('email',$email)->where(function ($query) {
            $query->orWhere('state','=','')
                  ->orWhere('country','=','')
                  ->orWhere('address','=','')
                  ->orWhere('specialty','=','');
        })->get();

最佳答案

首先,在SQL中按NULL进行过滤应该使用where x IS NOT NULL
对于laravel部分,Eloquent有一个whereNull方法,请参见:https://laravel.com/docs/5.6/queries
因此,您雄辩的代码应类似于:

$doctor= Doctor::where('email',$email)->where(function ($query) {
        $query->orWhereNull('state')
              ->orWhereNull('country')
              ->orWhereNull('address')
              ->orWhereNull('specialty');
    })->get();

关于php - 您如何在laravel 5.4中编写此sql查询?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51749946/

10-11 03:19
查看更多