nellytadi
nellytadi

Reputation: 101

How do you write this sql query in laravel 5.4?

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

This is the code i have but its not working properly. What i want is to return the row if any of the stated columns has its value as null.

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

Upvotes: 0

Views: 43

Answers (1)

matiit
matiit

Reputation: 8017

First things first - in SQL to filter by NULL one should be using where x IS NOT NULL.

For the laravel part, Eloquent has a whereNull method, see more: https://laravel.com/docs/5.6/queries

So your eloquent code should look somehing like:

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

Upvotes: 1

Related Questions