Sometimes one condition is not enough to fetch data from the database in the Laravel application. In this case, we need to attach one more where condition in the query. Suppose you have made a query that needs to be passed multiple where conditions, Now you do not know how to write a query in Laravel with where condition and even more with multiple where condition.

In this Laravel multiple where tutorial, we will take a look at how to use multiple where conditions in Laravel application with the query builder and with eloquent orm. We will use a simple query to test Laravel where multiple conditions.

First, we will see a simple where condition and then we will dive into the details of multiple where condition. Let's see how to write a query with multiple where condition in Laravel 10.

Now let's first see the very first example of where condition in laravel:

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

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

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

 

We can pass any dynamic or static data to make a condition with Laravel using where(). Let's see the multiple where condition example:

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

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

class TutorialController extends Controller
{
    public function index()
    {
      return User::where('status',true)->where('country','USA')->get();
    }
}

 

We can also write a query with multiple where conditions in Laravel using the query builder. Let's see the example of multiple where clause in laravel query builder:

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

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

class TutorialController extends Controller
{
    public function index()
    {
      return DB::table('users')->where('status',true)->where('country','USA')->get();
    }
}

 

Read also: Multiple Ways To Get Latest Records In Laravel

 

Conclusion

I have tried to demonstrate the concept of multiple where clause in laravel query builder in the Laravel application. In this tutorial, we have seen multiple where condition in laravel 10. Now we can say that we know how to use multiple where conditions in laravel.