Laravel 12 E-Commerce: Implementing Products Filter By Brand
This tutorial covers the steps for implementing product filtering by brand on the shop page by updating the `ShopController` and `shop.blade.php` view.
1. Update ShopController to Handle Brand Filter
The `index` function in `ShopController.php` is modified to fetch all brands and apply a `WHERE IN` condition to the product query based on the comma-separated `brands` value from the query string.
Fetch Brands and Get Filter Parameter
public function index(Request $request)
{
// Existing sorting and size logic...
$size = $request->query("size") ? $request->query("size") : 12;
$order = $request->query("orderby") ? $request->query("orderby") : 1;
// ... switch case logic for $o_column and $o_order ...
// Fetch all brands to display on the sidebar
$brands = Brand::orderBy("name","ASC")->get();
// Get the brand filter parameter from the query string
$f_brands = $request->query("brands"); // e.g., "1,5,10"
// 1. Apply Brand Filter Condition
$products = Product::where(function($query) use($f_brands) {
// If brands are selected ($f_brands is not empty), filter by brand_id
// Otherwise (orWhereRaw), if $f_brands is empty, the filter is effectively skipped,
// allowing all products to be returned.
$query->whereIn("brand_id", explode(",", $f_brands))
->orWhereRaw(""" . $f_brands . ""=""");
})
->orderBy($o_column, $o_order)
->paginate($size);
// Return the brands and the active filter value to the view
return view("shop", compact("products", "size", "order", "brands", "f_brands"));
}
2. Update `shop.blade.php` for Brand Checkboxes
In `shop.blade.php`, the Brands section is updated to display a list of all brands retrieved from the controller. Checkboxes are added, and JavaScript is included to handle the selection and form submission.
Brand Filter HTML (Sidebar)
<div class="sidebar-widget">
<h4 class="widget-title">Filter By Brands</h4>
<div class="widget-checkbox widget-categories">
<ul class="checkbox-items">
@foreach ($brands as $brand)
<li>
<input type="checkbox"
id="brand{{ $brand->id }}"
name="brands"
value="{{ $brand->id }}"
class="chk-brand"
{{-- Check if this brand'sID is in the comma-separated $f_brands string --}}
@if($f_brands && in_array($brand->id, explode(",", $f_brands))) checked="checked" @endif
>
<label for="brand{{ $brand->id }}">
<span></span>{{ $brand->name }}
</label>
</li>
@endforeach
</ul>
</div>
</div>
Hidden Input in Filter Form
A hidden input field is added to the main filtering form (`#frmfilter`) to store the comma-separated list of selected Brand IDs before submission.
<!-- Inside the main filtering form with id="frmfilter" -->
<input type="hidden" name="brands" id="hdnBrands" value="{{$f_brands}}">
JavaScript for Checkbox Logic
JavaScript is added to listen for changes on the brand checkboxes, compile the selected brand IDs into a comma-separated string, update the hidden field, and submit the form.
@push("scripts")
<script>
// Event listener for Brand checkboxes
$(".chk-brand").on("change", function() {
var brands = "";
// Loop through all checkboxes with class "chk-brand"
$(".chk-brand").each(function() {
if ($(this).is(":checked")) {
// Compile selected brand IDs into a comma-separated string
if (brands === "") {
brands += $(this).val();
} else {
brands += "," + $(this).val();
}
}
});
// Set the value of the hidden input field
$("#hdnBrands").val(brands);
// Submit the form to apply the filter
$("#frmfilter").submit();
});
</script>
@endpush
This implementation allows users to select one or more brands to filter the products displayed on the shop page.
