Advanced Search Filter using Dropdown in Laravel

Hello Artisan

In this tutorial i will discuss about laravel advanced eloquent filter example with multiple model or table. I will explain laravel multiple model advanced eloquent filter example. Sometime we need to create filter on multiple table in laravel. So in this tutorial i will show you how we can make multiple filter with only single query.

So if you don't know laravel search query with multiple conditions then this tutorial will be the perfect example for you. We will create products, color and price table and will create filter using price and color. According to price and color we will fetch product from database. This tutorial will be the dropdown filter in laravel.

I will discuss step by step and i will start from scratch. So you need a fresh laravel application or you can implement it with your ongoing project. So let's start our laravel advanced search query filter example. Let's create laravel dropdown filter example.

See the below image of Laravel dropdown filter example with multiple table.

Before select price and color

laravel-eloquent-search-query

 

When user select only blue

laravel-dropdown-filter-example

 

When user select both parameter

laravel-search-filter-query

 

Step 1: Download Laravel 

As we are going to create laravel filter example from scratch, so download it by the following command.

composer create-project --prefer-dist laravel/laravel blog

 

Step 2:  Setup Migration

Now we need three model like Product, Color and Price to create laravel filter query example tutorial. So create model by the following command.

php artisan make:model Product -m
php artisan make:model Price -m
php artisan make:model Color -m

 

After running those command, paste this below code to those file one after another.

app/Product.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    public function price()
    {
    	return $this->hasOne(Price::class);
    }
    public function color()
    {
    	return $this->hasOne(Color::class);
    }
}

 

app/Color.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Color extends Model
{
    //
}

 

app/Price.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Price extends Model
{
    //
}

 

Create belongsTo relationship here if you need to fetch data for your app. But this is a demo tutorial that's why i didn't do it. Now check migration file.

database/migration/create_products_table.php

    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
    }

 

database/migration/create_color_table.php

 public function up()
    {
        Schema::create('colors', function (Blueprint $table) {
            $table->id();
            $table->tinyInteger('product_id');
            $table->string('name');
            $table->timestamps();
        });
    }

 

database/migration/create_price_table.php

 public function up()
    {
        Schema::create('prices', function (Blueprint $table) {
            $table->id();
            $table->tinyInteger('product_id');
            $table->double('price');
            $table->timestamps();
        });
    }

 

Step 3:  Setup Route

We new one route to view our filter page. So paste this code in the following path.

routes/web.php

use App\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::get('/', function (Request $request) {
    
    $product = Product::where( function($query) use($request){
                     return $request->price_id ?
                            $query->from('prices')->where('id',$request->price_id) : '';
                })->where(function($query) use($request){
                     return $request->color_id ?
                            $query->from('colors')->where('id',$request->color_id) : '';
                })
                ->with('price','color')
                ->get();
     
    $selected_id = [];
    $selected_id['price_id'] = $request->price_id;
    $selected_id['color_id'] = $request->color_id;

    return view('test',compact('product','selected_id'));

})->name('filter');

 

Read also : Laravel Pipeline Interpretation with Example

 

Step 4:   Setup blade file

Now in the final step, we need to create test.blade.php file. So create ti to make laravel advanced search filter example tutorial. 

resources/views/test.blade.php

 

Recommended : Active Inactive Filter Example Using Vue Js in Laravel

 

Now filtering data in laravel tutorial is completed. Now you can check it by visiting the root url. Hope it can help you to create laravel search filter query.