How To Filter In Laravel Query Example

  • 30-09-2022
  • Laravel 9
  • Haresh Chauhan

How To Filter In Laravel Query Example? In this post I will give you some examples of laravel eloquent queries, There are many different ways to you can filter in the laravel application.

Filtering in laravel query is the most important part while you when working with large data collection. That time you just need to filter the data according to your analysis. also in the admin panel sorting and analyzing data is an important part of data listing.

So in this post, I will show you some best tips for applying search query filters in the laravel application. Laravel provides scops for filtering the data from the database. and provided when the method to check it out. In all the methods in this post, I will teach you how to apply the laravel eloquent query.

Example 1. Using when() Method

In this example we will use when() method. This method will ask the first argument as a condition when you want to filter, and the second argument for the callback function with the where clauses. Check the below example. The request I am checking with the given key. if the key exists in the request, this will allow you to run the next apply where clauses with the like query.

use App\Models\Order;

public function getOrder(Request $request)
    $order = Order::select(
    ->when(isset($request->order_id),function($query) use ($request){
    ->when(isset($request->mobile),function($query) use ($request){
    ->when(isset($request->order_date),function($query) use ($request){
    ->when(isset($request->payment_status),function($query) use ($request){

Example 2. Using If Condition

We can also use the if condition instead of the when laravel method, We can check the request available keys, If the request has a key that we define then the if condition will be true and filter the inside the where clause.

In this example, I am using ilike query for search filter find from the database. If you are using MYSQL you must need to use the LIKE query for the filter.

use App\Models\Clinic;

public function getClinicList(Request $request)
    $clinic = Clinic::select(


        $clinic->where('name', 'ilike', "%{$request->name}%");


        $clinic->orderBy('success_patient', 'desc');

        $clinic->where('mobile', 'ilike', "%{$request->mobile}%");


        $clinic->where('email', 'ilike', "%{$request->email}%");


    $clinic = $clinic->paginate(10);

Example 3. Using Scope

In this filter example, we will pre-define the filter in the model, we will use the scope in the filter method, which we will define in the model.

While you filter the query you just need to call the scope method from the model. You can also pass an argument inside the scope method. Below are examples of both methods, with argument and simple scope filter method.

use App\Models\Order;

public function getOrder($status,Request $request)
    $orders = Order::query()
        ->when(isset($request->active), function ($query){
            return $query->active();
        ->when($status == 'on_going', function ($query) {
            return $query->Ongoing('on_going');

Define the scope method inside the model, The method we will call in the controller. This method will work as a filter in your query. So this is the latest filtering common method in the latest laravel filtering method.


namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
    protected $guarded = [];
    public function scopeOngoing($query,$status)
        return $query->where('order_status',$status);

    public function scopeActive($query)
        return $query->where('status',1);

We always thanks to you for reading our blogs.


Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us


Haresh Chauhan


We Are Also Recommending You :