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

how to add multiple select in laravel eloquent ?

December 29, 2021February 22, 2024

Laravel eloquent builder has rich features to modify the query. In some cases we wanted to add multiple select in laravel and modify the select statement on basis of few conditions and wanted to add some select statement on condition. so in this article we will learn to add multiple…

Read More

How to enable cors in web api php or laravel?

August 27, 2021August 20, 2022

Cross-Origin Resource Sharing (CORS) is mechanism which allow browser to share the resources between the other domain or port. if the cors is disabled in api or server then other domain can’t access the apis and resource of server. Example: if a server A wants to access the apis of server…

Read More
Php Laravel match password with hash string

How to check password with hashed string in laravel 8 ?

January 26, 2022January 26, 2022

Laravel hashed password with bcrypt algorithm is not decryptable and to match the hashed string with plain string we use Hash::check method. For hashing the password laravel use secure Bcrypt and Argon2 hashing for storing user passwords. So in this tutorial i will show you to check password with hashed…

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

  • 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

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version