Laravel Ecommerce Product sorting Process
//product sorting
Route::get('/products/sort/price-low-to-high', 'sortByPriceLowToHigh')->name('products.sort.price_low_to_high');
Route::get('/products/sort/price-high-to-low', 'sortByPriceHighToLow')->name('products.sort.price_high_to_low');
Route::get('/products/sort/popularity', 'sortByPopularity')->name('products.sort.popularity');
Route::get('/products/sort/latest', 'sortByLatest')->name('products.sort.latest');
//product sorting
public function sortByPriceLowToHigh()
{
$products = Product::orderBy('selling_price', 'asc')->get(); // Sort products by price low to high
return view('front.product.shop', compact('products'));
}
public function sortByPriceHighToLow()
{
$products = Product::orderBy('selling_price', 'desc')->get(); // Sort products by price high to low
return view('front.product.shop', compact('products'));
}
//blade file code
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle text-black" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Default Sorting
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="{{ route('products.sort.price_low_to_high') }}">Sort By Price: low to high</a></li>
<li><a class="dropdown-item" href="{{ route('products.sort.price_high_to_low') }}">Sort By Price: high to low</a></li>
<li><a class="dropdown-item" href="{{ route('products.sort.popularity') }}">Sort By Popularity</a></li>
<li><a class="dropdown-item" href="{{ route('products.sort.latest') }}">Sort By Latest</a></li>
</ul>
</div>
No comments