Skip to content

Commit 2ce5178

Browse files
committed
Implement sorting of products
1 parent 4a08af6 commit 2ce5178

File tree

2 files changed

+61
-16
lines changed

2 files changed

+61
-16
lines changed

app/Http/Controllers/ProductController.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,25 @@ public function view(Product $product)
3636
private function renderProducts(Builder $query)
3737
{
3838
$search = \request()->get('search');
39+
$sort = \request()->get('sort', '-updated_at');
40+
41+
if ($sort) {
42+
$sortDirection = 'asc';
43+
if ($sort[0] === '-') {
44+
$sortDirection = 'desc';
45+
}
46+
$sortField = preg_replace('/^-?/', '', $sort);
47+
48+
$query->orderBy($sortField, $sortDirection);
49+
}
3950
$products = $query
4051
->where('published', '=', 1)
4152
->where(function ($query) use ($search) {
4253
/** @var $query \Illuminate\Database\Eloquent\Builder */
4354
$query->where('products.title', 'like', "%$search%")
4455
->orWhere('products.description', 'like', "%$search%");
4556
})
46-
->orderBy('updated_at', 'desc')
57+
4758
->paginate(5);
4859

4960
return view('product.index', [

resources/views/product/index.blade.php

+49-15
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,58 @@
77
<x-app-layout>
88
<x-category-list :category-list="$categoryList" class="-ml-5 -mt-5 -mr-5 px-4"/>
99

10-
<div class="p-3 pb-0">
11-
<form action="" method="GET">
12-
<x-input type="text" name="search" placeholder="Search for the products" value="{{request()->get('search')}}" />
10+
<div class="flex gap-2 items-center p-3 pb-0" x-data="{
11+
selectedSort: '{{ request()->get('sort', '-updated_at') }}',
12+
searchKeyword: '{{ request()->get('search') }}',
13+
updateUrl() {
14+
const params = new URLSearchParams(window.location.search)
15+
if (this.selectedSort && this.selectedSort !== '-updated_at') {
16+
params.set('sort', this.selectedSort)
17+
} else {
18+
params.delete('sort')
19+
}
20+
21+
if (this.searchKeyword) {
22+
params.set('search', this.searchKeyword)
23+
} else {
24+
params.delete('search')
25+
}
26+
window.location.href = window.location.origin + window.location.pathname + '?'
27+
+ params.toString();
28+
}
29+
}">
30+
<form action="" method="GET" class="flex-1" @submit.prevent="updateUrl">
31+
<x-input type="text" name="search" placeholder="Search for the products"
32+
x-model="searchKeyword"/>
1333
</form>
34+
<x-input
35+
x-model="selectedSort"
36+
@change="updateUrl"
37+
type="select"
38+
name="sort"
39+
class="w-full focus:border-purple-600 focus:ring-purple-600 border-gray-300 rounded">
40+
<option value="price">Price (ASC)</option>
41+
<option value="-price">Price (DESC)</option>
42+
<option value="title">Title (ASC)</option>
43+
<option value="-title">Title (DESC)</option>
44+
<option value="-updated_at">Last Modified at the top</option>
45+
<option value="updated_at">Last Modified at the bottom</option>
46+
</x-input>
47+
1448
</div>
1549

16-
<?php if ($products->count() === 0): ?>
17-
<div class="text-center text-gray-600 py-16 text-xl">
18-
There are no products published
19-
</div>
50+
<?php if ( $products->count() === 0 ): ?>
51+
<div class="text-center text-gray-600 py-16 text-xl">
52+
There are no products published
53+
</div>
2054
<?php else: ?>
21-
<div
22-
class="grid gap-4 grig-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 p-3"
23-
>
24-
@foreach($products as $product)
25-
<!-- Product Item -->
26-
<div
27-
x-data="productItem({{ json_encode([
55+
<div
56+
class="grid gap-4 grig-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 p-3"
57+
>
58+
@foreach($products as $product)
59+
<!-- Product Item -->
60+
<div
61+
x-data="productItem({{ json_encode([
2862
'id' => $product->id,
2963
'slug' => $product->slug,
3064
'image' => $product->image ?: '/img/noimage.png',
@@ -59,6 +93,6 @@ class="object-cover rounded-lg hover:scale-105 hover:rotate-1 transition-transfo
5993
<!--/ Product Item -->
6094
@endforeach
6195
</div>
62-
{{$products->links()}}
96+
{{$products->appends(['sort' => request('sort'), 'search' => request('search')])->links()}}
6397
<?php endif; ?>
6498
</x-app-layout>

0 commit comments

Comments
 (0)