Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
laravel multiple where condition in eloquent

How to use laravel multiple where condition with example ?

Aman Jain, March 5, 2022November 8, 2023

In this article i will show you to use laravel multiple where condition in 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.

Example of laravel multiple where condition

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 ?

Related

Php Laravel laravelmultiplephpwhere

Post navigation

Previous post
Next post

Related Posts

Php How to Upload image with preview in Laravel 5/6 with example

How to Upload image with preview in Laravel 5/6 with example ?

June 12, 2022June 25, 2022

In this tutorial we will learn to Upload image with preview in laravel or for any website can be basic requirement like set up a product picture or adding profile to for verification. Laravel 5/6 provides robust functionality to upload and process the image with security. Laravel simply use Request…

Read More
Php How to Call an External Url API in Laravel

How to Call an External Url API in Laravel ?

June 5, 2022June 5, 2022

In this article we will learn to call an external Url API in Laravel. Whenever we want to access the third party data we need to access the data using the APIs, we send a request to another server means outside our application and they respond with preformatted structure. In…

Read More
Php How to Change Date Format in Blade View File in Laravel

How to Change Date Format in Blade View File in Laravel ?

September 8, 2022March 16, 2024

Laravel default shows the date in the way its stored in database bu sometimes we want to change date format in blade view file in laravel according to our requirements. It can be multiple possibilities and requirement from client or product manager to change the date format so in this…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version