Skip to content

Commit 4a08af6

Browse files
committed
Implement searching for products on website
1 parent 850d665 commit 4a08af6

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

app/Http/Controllers/ProductController.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,51 @@
44

55
use App\Models\Category;
66
use App\Models\Product;
7+
use Illuminate\Database\Eloquent\Builder;
78
use Illuminate\Http\Request;
89

910
class ProductController extends Controller
1011
{
1112
public function index()
1213
{
13-
$products = Product::query()
14-
->where('published', '=', 1)
15-
->orderBy('updated_at', 'desc')
16-
->paginate(5);
17-
return view('product.index', [
18-
'products' => $products
19-
]);
14+
$query = Product::query();
15+
16+
return $this->renderProducts($query);
2017
}
2118

2219
public function byCategory(Category $category)
2320
{
2421
$categories = Category::getAllChildrenByParent($category);
2522

26-
$products = Product::query()
23+
$query = Product::query()
2724
->select('products.*')
2825
->join('product_categories AS pc', 'pc.product_id', 'products.id')
26+
->whereIn('pc.category_id', array_map(fn($c) => $c->id, $categories));
27+
28+
return $this->renderProducts($query);
29+
}
30+
31+
public function view(Product $product)
32+
{
33+
return view('product.view', ['product' => $product]);
34+
}
35+
36+
private function renderProducts(Builder $query)
37+
{
38+
$search = \request()->get('search');
39+
$products = $query
2940
->where('published', '=', 1)
30-
->whereIn('pc.category_id', array_map(fn($c) => $c->id, $categories))
41+
->where(function ($query) use ($search) {
42+
/** @var $query \Illuminate\Database\Eloquent\Builder */
43+
$query->where('products.title', 'like', "%$search%")
44+
->orWhere('products.description', 'like', "%$search%");
45+
})
3146
->orderBy('updated_at', 'desc')
3247
->paginate(5);
3348

3449
return view('product.index', [
3550
'products' => $products
3651
]);
37-
}
3852

39-
public function view(Product $product)
40-
{
41-
return view('product.view', ['product' => $product]);
4253
}
4354
}

resources/views/product/index.blade.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@
66

77
<x-app-layout>
88
<x-category-list :category-list="$categoryList" class="-ml-5 -mt-5 -mr-5 px-4"/>
9+
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')}}" />
13+
</form>
14+
</div>
15+
916
<?php if ($products->count() === 0): ?>
1017
<div class="text-center text-gray-600 py-16 text-xl">
1118
There are no products published
1219
</div>
1320
<?php else: ?>
1421
<div
15-
class="grid gap-8 grig-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 p-5"
22+
class="grid gap-4 grig-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 p-3"
1623
>
1724
@foreach($products as $product)
1825
<!-- Product Item -->

0 commit comments

Comments
 (0)