Spread the love

In this article i will show you to use multiple where condition in Laravel eloquent. In laravel we can create multiple where condition in several ways like using the array of array, associative column and value and using multiple where clause. Sometimes we wanted to use multiple and condition or multiple where or condition in laravel eloquent so in that case we can use where parameters with multidimensional array , associative array.

Here is the simple syntax to use the laravel multiple where condition

Syntax to use

Model:where('COLUMN_NAME', 'OPERATOR', 'VALUE')->
where('COLUMN_NAME2', 'OPERATOR', 'VALUE2');

//or
 
Model:where([
["COLUMN_NAME","OPERATOR","VALUE"],
["COLUMN_NAME2","OPERATOR","VALUE2"],
]);

//or
Model:where( 
["COLUMN_NAME"=>"VALUE","COLUMN_NAME2"=>"VALUE2"]
);

//or

whereRaw('RAW_QUERY_USING_DB_FACADE')

Sql of above query will be

select * from `table` where `COLUMN_NAME` = 'VALUE' and COLUMN_NAME2='VALUE2'

As you can see i have written multiple way to use same method with different parameters. So lets begin with simple examples

Example 1 – Multiple where using Multidimensional array

In this example i will show you to create multiple where using multidimensional array

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Article;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $Article = Article::select("*")
                        ->where([
                           ["title","=",'test'],
                           ["status","=",'1'],
                         ])
                        ->get();
   }
}

Output will be

select * from `articles` where `title` = 'test' and `status` = 1

You can hold the array in variable and can apply conditions before adding it to array as below

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Article;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $whereMultiple = [
            ["title", "=", 'test']
        ];
        if ($request->get("filterStatus",'0') == '1') {
            $whereMultiple[] = ["status", "=", '1'];
        }
        
        $Article = Article::select("*")
            ->where($whereMultiple)
            ->get();
   }
}

Example 2 – Multiple where using associative array

In this example we will multiple where using associative array and sometimes we also want to apply conditions before adding the where condition so you check below example

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Article;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $whereMultiple = [ "title"=> 'test'];
       
        if ($request->get("filterStatus",'0') == '1') {
            $whereMultiple['status'] =   '1';
        }
        
        $Article = Article::select("*")
            ->where($whereMultiple)
            ->get();
   }
}

Output

select * from `articles` where `title` = 'test' and `status` = '1'

Example 3 – Multiple where with or condition

Sometimes we wanted to apply and condition and or condition both together and also want to group the or so we can use below example

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Article;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
       $whereMultiple = [ "title"=> 'test'];
       
        if ($request->get("filterStatus",'0') == '1') {
            $whereMultiple['status'] =   '1';
        }
        
        $Article = Article::select("*")
            ->where("email","test@yopmail.com")
            ->orWhere($whereMultiple)
            ->get();

   }
}

Output

select * from `articles` where `email` = test@yopmail.com or (`title` = test or `status` = 1)

Also Read : How to use conditional where clause in Laravel 8 eloquent ?

Leave a Reply