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 Copy Folder Recursively One Folder to Another in Laravel

How to Copy Folder Recursively One Folder to Another in Laravel ?

May 26, 2022May 26, 2022

In this article i will help you to copy the folders recursively from one folder to another in laravel .File System is used to perform the file system based operations like copy Folder Recursively. For this purpose laravel uses Illuminate\Filesystem\Filesystem class in laravel. Laravel provides inbuilt library to access the…

Read More
Php How to Get Last Record With Group By in Laravel Eloquent

How to Get Last Record With Group By in Laravel Eloquent ?

September 7, 2022March 16, 2024

In this tutorial we will learn to get last record with group by in laravel eloquent. while working with group by in mysql its gives first record from the group by records that means if you have multiple record with same id and you want the most recent record or…

Read More
Php Static keyword

What is static keyword and properties in a php/java class?

June 13, 2021September 21, 2021

Static keyword are isolated, which means we can access the property of a class without creating a object/instance of class. static methods that are common to all the objects of the class. Hence, any logic which can be shared among multiple instances of a class should be inside the static…

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

  • 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 Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version