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

Share this

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.
Share this

Leave a Comment