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

Devops How to Install multiple versions of PHP?

How to Install multiple versions of PHP?

September 18, 2021September 21, 2021

Sometimes it’s required to install the different version of PHP rather then the default one of Ubuntu Repository System. To install the different version we will follow the basic steps before installation of PHP Step 1 : Add ppa:ondrej/php to Ubuntu system Open the terminal or ssh connection and then…

Read More
Laravel append URL query params to pagination laravel

How to Append URL Query Params to Pagination Laravel ?

November 17, 2023March 16, 2024

In this guide, we’ll focus on improving user experience by append URL query params to pagination laravel links. Pagination plays a vital role in web applications, enabling users to navigate through extensive datasets.This not only enhances usability but also ensures a smoother transition between pages. Append URL query params to…

Read More
Php How to change data type of column in laravel migration

How to change data type of column in laravel migration ?

February 20, 2022November 5, 2023

In this blog post we will learn to change data type of column in laravel migration. Laravel covers most of the migration features like add, delete, indexing etc. but to modify the table like renaming column, change data type column to existing table laravel uses a separate package doctrine/dbal. In…

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

  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • 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
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version