Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Laravel where or condition with example

How to use laravel where or condition with example ?

Aman Jain, January 15, 2022February 19, 2022

Laravel eloquent provides multiple ways to build the query one the of the feature of laravel eloquent is creating dynamic query based on condition or complicated queries.In this article i will show you to build where or condition in laravel with example. I will show you multiple example to create where or query condition and also the complicated queries.

Here is the simple syntax to use the laravel where query

Syntax to use

orWhere('COLUMN_NAME', 'OPERATOR', 'VALUE')

//or
 
orWhere('COLUMN_NAME', 'VALUE'); // in this case operator always will be `=`

//or

orWhere('RAW_QUERY_USING_DB_FACADE')

//OR 

orWhereColumnName('VALUE');

SQL of above query will be

select * from `table` where `COLUMN_NAME` = 'VALUE'

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

Example 1 – Simple orWhere example

In this example i will show you simple where condition with two parameters

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

   }
}

Output will be

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

Example 2 – Passing operator to orWhere

In this example we will use operator like greater then, less then, equals to, like etc.

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

      
   }
}

Output

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

Example 3 – Multiple where condition

In this example we will use operator like greater then, less then, equals to, like etc.

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

      $Article = Article::select("*")
                        ->where("title","like" ,'test')
                        ->orWhere("status", 1)
                        ->orWhere("is_active", 1)
                        ->get();
      

   }
}

Output

select * from `articles` where `title` like 'test' or `status` = 1 or `is_active` = 1

Example 4 – Complicated Raw query in where

In this example we will use DB::raw to use complicated query in our laravel application. Sometimes we wanted to use aggregate function in laravel where so

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

Output

select * from `articles` where `status` = 1 or concat(first_name,'', last_name ) = 'test test2' 

Example 5 – Add brackets to or where condition

In this example we will use DB::raw to use complicated query in our laravel application. Sometimes we wanted to use aggregate function in laravel where so

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

      $Article = Article::select("*")
                        ->where("title","test")
                        ->where(function($q){
                          return $q->where("status",1)->orWhere('is_active',1);
                        })
                         ->get();

   }
}

Output

select * from `articles` where `title` = 'test test2' or (`status` = 1 and `is_active` = 1 ) 

#or

select * from `articles` where `title` = 'test test2' and (`status` = 1 or `is_active` = 1 ) 

Related

Php Laravel laravelorphpwhere

Post navigation

Previous post
Next post

Related Posts

Php Laravel 10 Image Upload with Preview Example

Laravel 10 Image Upload with Preview Example

March 7, 2023March 16, 2024

In this blog post we will learn laravel image upload. Laravel 10 offers robust functionality for uploading and processing images, which is often a basic requirement for websites, ranging from setting up a profile picture to providing necessary documents. With Laravel 10, image uploads can be handled with enhanced security…

Read More
Php Laravel orderBy clause in eloquent with example

Laravel orderBy clause in eloquent with example

October 5, 2022March 16, 2024

In this blog post, we will take a look at how to use the Laravel orderBy clause in the Eloquent ORM.Laravel provides an expressive, fluent interface for creating and retrieving records in your database. When we work with databases in Laravel, we often need to order the results we get…

Read More
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

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