How To Filter In Laravel Query Example

  • 30-09-2022
  • 954
  • 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(
        'id',
        'address',
        'scheduled_time',
        'order_date',
        'payment_mode',
        'item',
        'status',
    )
    ->when(isset($request->order_id),function($query) use ($request){
        $query->where('order_id','ilike',"%{$request->order_id}%");
    })
    ->when(isset($request->mobile),function($query) use ($request){
        $query->where('mobile','ilike',"%{$request->mobile}%");
    })
    ->when(isset($request->order_date),function($query) use ($request){
        $query->whereDate('order_date',$request->order_date);
    })
    ->when(isset($request->payment_status),function($query) use ($request){
        $query->where('payment_status',$request->payment_status);
    })
    ->orderBy('created_at','desc')
    ->paginate(10);
}

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(
        'id',
        'name',
        'email',
        'mobile',
        'username',
    );

    if($request->has('name')){

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

    if($request->has('success_patient')){

        $clinic->orderBy('success_patient', 'desc');
    }
  
    if($request->has('mobile')){

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

    if($request->has('email')){

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

    if($request->has('status')){

        $clinic->where('status',$request->status);
    }
    
    $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.

App\Http\Controllers\HomeController.php
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');
        })
        ->paginate(10);
}

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.

App\Models\Order.php
<?php

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-image

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-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :