General

Laravel-How can I do a filter from multiple dropdown select on the view using one single query

Ravindra Kumar

To implement a filter system using multiple dropdown selects and a search bar with a single query, here’s a structured approach using Laravel’s Eloquent model:

Step 1: Form in Blade View

Create a form with input fields for the search bar and dropdown selects.


<form method="GET" action="{{ route('filter') }}">
<input type="text" name="search" placeholder="Search..." value="{{ request('search') }}">

<select name="category">
<option value="">Select Category</option>
@foreach ($categories as $category)
<option value="{{ $category->id }}" {{ request('category') == $category->id ? 'selected' : '' }}>
{{ $category->name }}
</option>
@endforeach
</select>

<select name="course">
<option value="">Select Course</option>
@foreach ($courses as $course)
<option value="{{ $course->id }}" {{ request('course') == $course->id ? 'selected' : '' }}>
{{ $course->name }}
</option>
@endforeach
</select>

<select name="country">
<option value="">Select Country</option>
@foreach ($countries as $country)
<option value="{{ $country->id }}" {{ request('country') == $country->id ? 'selected' : '' }}>
{{ $country->name }}
</option>
@endforeach
</select>

<select name="state">
<option value="">Select State</option>
@foreach ($states as $state)
<option value="{{ $state->id }}" {{ request('state') == $state->id ? 'selected' : '' }}>
{{ $state->name }}
</option>
@endforeach
</select>

<select name="district">
<option value="">Select District</option>
@foreach ($districts as $district)
<option value="{{ $district->id }}" {{ request('district') == $district->id ? 'selected' : '' }}>
{{ $district->name }}
</option>
@endforeach
</select>

<select name="city">
<option value="">Select City</option>
@foreach ($cities as $city)
<option value="{{ $city->id }}" {{ request('city') == $city->id ? 'selected' : '' }}>
{{ $city->name }}
</option>
@endforeach
</select>

<button type="submit">Filter</button>
</form>

Step 2: Backend Logic in Controller

In your controller, use the model to dynamically apply filters based on the user’s input.


public function filter(Request $request)
{
// Start with the base query
$query = ModelName::query(); // Replace ModelName with your actual model

// Apply search filter
if ($request->filled('search')) {
$query->where('name', 'like', '%' . $request->input('search') . '%');
}

// Apply category filter
if ($request->filled('category')) {
$query->where('category_id', $request->input('category'));
}

// Apply course filter
if ($request->filled('course')) {
$query->where('course_id', $request->input('course'));
}

// Apply country filter
if ($request->filled('country')) {
$query->where('country_id', $request->input('country'));
}

// Apply state filter
if ($request->filled('state')) {
$query->where('state_id', $request->input('state'));
}

// Apply district filter
if ($request->filled('district')) {
$query->where('district_id', $request->input('district'));
}

// Apply city filter
if ($request->filled('city')) {
$query->where('city_id', $request->input('city'));
}

// Execute the query
$results = $query->get();

// Pass data to the view
return view('your_view_file', compact('results'));
}

Step 3: Route Setup

Define a route to handle the form submission and display the filtered results.


Route::get('/filter', [YourController::class, 'filter'])->name('filter');

Step 4: Blade File to Display Results

Use the $results variable passed from the controller to display the filtered data.


@if($results->isEmpty())
<p>No results found.</p>
@else
<table>
<thead>
<tr>
<th>Name</th>
<th>Category</th>
<th>Course</th>
<th>Location</th>
</tr>
</thead>
<tbody>
@foreach ($results as $result)
<tr>
<td>{{ $result->name }}</td>
<td>{{ $result->category->name }}</td>
<td>{{ $result->course->name }}</td>
<td>{{ $result->city->name }}, {{ $result->state->name }}, {{ $result->country->name }}</td>
</tr>
@endforeach
</tbody>
</table>
@endif

Step 5: Optimize Database Relations

Ensure your model relationships are properly defined in ModelName:


public function category() {
return $this->belongsTo(Category::class);
}

public function course() {
return $this->belongsTo(Course::class);
}

public function country() {
return $this->belongsTo(Country::class);
}

public function state() {
return $this->belongsTo(State::class);
}

public function district() {
return $this->belongsTo(District::class);
}

public function city() {
return $this->belongsTo(City::class);
}

Result

  • Users can filter data dynamically.
  • One single query fetches all filtered results efficiently.
  • Clean and maintainable code using Eloquent models and relationships.

Ravindra Kumar

Ravindra is a passionate full stack developer and dedicated blogger with a flair for crafting user-friendly web applications and insightful articles. With expertise spanning front-end and back-end technologies, Ravindra brings ideas to life through innovative coding solutions.

Suggested Reading

Download Free Pets & Animals Website Template

Create a Perfect Online Home for Your Pet Store or Animal Care Brand This Pets & Animals Website Template is designed for pet shops, grooming centers, veterinary clinics, animal shelters, and pet bloggers. With a cheerful layout, responsive design, and simple customization, it’s ideal for businesses that care for pets and animals with love. Template […]

eLearning Platform Website Template using HTML, CSS, Tailwind free Download

Looking to build a modern and responsive eLearning platform or online course website? Our eLearning Platform Template is designed using HTML, CSS, and Tailwind CSS, giving you a lightweight, fast, and customizable solution – and the best part, it’s absolutely free to download. Key Features Responsive Design – Built with Tailwind CSS for seamless viewing […]

Forgot Password in PHP Using AJAX and MySQL

User authentication is one of the most important parts of any web application. While creating a login and registration system in PHP is quite common, many developers forget to implement a secure Forgot Password feature. A Forgot Password system allows users to reset their password in case they lose access. In this article, we’ll build […]

(7)+ Platforms to Build AI Agents in 2025: A Complete Guide

Artificial Intelligence (AI) is no longer just about chatbots and smart recommendations. Today, AI agents are reshaping the way we work—automating tasks, reasoning through decisions, and collaborating like digital teammates. If you’re looking to create AI-powered agents for business or personal productivity, a growing number of platforms make it possible without heavy coding knowledge. In […]

The Most Reliable Hosting Options for Startups and SMBs

Small businesses are the backbone of any country. India accounts for over 63 million SMBs (Small and Medium Businesses) [ET Times]. Stepping up into this sector has its challenges, including a tight budget, growing demands, and the constant requirement to stay online. This is where a web hosting service becomes non-negotiable. Whether launching the first […]

Ecommerce Website Frontend source Code in HTML,CSS,Javascript & Bootstrap

Front-end source code makes up an eCommerce website’s UI. This code is crucial to buyer-site interaction.  It includes the structural, visual, and engaging parts that make the user’s experience unique. Online shopping Website frontend source code was created with HTML, CSS, JavaScript, and different frameworks, ensuring the website works and looks good. HTML: Structuring the Web Pages […]