Whether you are using Laravel Eloquent or query builder, it does not matter. We can use as many as where conditions in Laravel query. In laravel query, laravel where is must needed thing. Cause without it, we can not fetch conditional data. So when we need to fetch conditional data, we need where() condition. 

In this tutorial, we will see how to use laravel where, laravel where not, laravel wherein, and laravel where eloquent with examples. We will see one by one with an example query.

laravel-where-orwhere-wherein-wherenot-wherenull

Let's see the first example with laravel where.

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TutorialController extends Controller
{
    public function __invoke()
    {
        return User::where('id', 1)->first();
    }
}

 

You can use get() also with where() like that:

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TutorialController extends Controller
{
    public function __invoke()
    {
        return User::where('city', 'dhaka')->get();
    }
}

 

You can use where with orWhere() in laravel. See the example query with where and orwhere laravel:

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TutorialController extends Controller
{
    public function __invoke()
    {
        return User::where('id', 1)
            ->orWhere('name', 'John')
            ->get();
    }
}

 

Now let's see the example of laravel where not:

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TutorialController extends Controller
{
    public function __invoke()
    {
        return User::where('city', '!=','dhaka')->get();
    }
}

 

To check empty for a specific field like laravel where not null, you can use where like that in Laravel:

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TutorialController extends Controller
{
    public function __invoke()
    {
        return User::where('field_name', '<>', '')->get();
    }
}

 

You can also use whereNull() to check null or empty values like:

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TutorialController extends Controller
{
    public function __invoke()
    {
        return User::whereNull('country_id')->get();
    }
}

 

laravel wherein is a little bit different. It accepts two parameters, one is a column and another is an array of values. 

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TutorialController extends Controller
{
    public function __invoke()
    {
        return User::whereIn('id', [1, 2, 3])->get();
    }
}

 

Read also: Laravel Subquery Join With Laravel DB Raw Example

 

Conclusion

We have tried to discuss all the queries related to laravel where. Now we know how to write queries with Laravel where, laravel orwhere, laravel whereNull, laravel wherein and also laravel where not. Hope this tutorial will help you.