Skip to content

Commit 9c6e174

Browse files
committed
Implement rendering categories on frontend and filter products by categories
1 parent 288ace9 commit 9c6e174

File tree

6 files changed

+64
-0
lines changed

6 files changed

+64
-0
lines changed

app/Http/Controllers/ProductController.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Models\Category;
56
use App\Models\Product;
67
use Illuminate\Http\Request;
78

@@ -18,6 +19,23 @@ public function index()
1819
]);
1920
}
2021

22+
public function byCategory(Category $category)
23+
{
24+
$categories = Category::getAllChildrenByParent($category);
25+
26+
$products = Product::query()
27+
->select('products.*')
28+
->join('product_categories AS pc', 'pc.product_id', 'products.id')
29+
->where('published', '=', 1)
30+
->whereIn('pc.category_id', array_map(fn($c) => $c->id, $categories))
31+
->orderBy('updated_at', 'desc')
32+
->paginate(5);
33+
34+
return view('product.index', [
35+
'products' => $products
36+
]);
37+
}
38+
2139
public function view(Product $product)
2240
{
2341
return view('product.view', ['product' => $product]);

app/Models/Category.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ public static function getActiveAsTree($resourceClassName = null)
3939
return self::buildCategoryTree($categories, null, $resourceClassName);
4040
}
4141

42+
public static function getAllChildrenByParent(Category $category)
43+
{
44+
$categories = Category::where('active', true)->orderBy('parent_id')->get();
45+
$result[] = $category;
46+
self::getCategoriesArray($categories, $category->id, $result);
47+
48+
return $result;
49+
}
50+
4251
private static function buildCategoryTree($categories, $parentId = null, $resourceClassName = null)
4352
{
4453
$categoryTree = [];
@@ -55,4 +64,14 @@ private static function buildCategoryTree($categories, $parentId = null, $resour
5564

5665
return $categoryTree;
5766
}
67+
68+
private static function getCategoriesArray($categories, $parentId, &$result)
69+
{
70+
foreach ($categories as $category) {
71+
if ($category->parent_id === $parentId) {
72+
$result[] = $category;
73+
self::getCategoriesArray($categories, $category->id, $result);
74+
}
75+
}
76+
}
5877
}

resources/css/app.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,12 @@ body {
8585
.table-sm > thead > tr > th {
8686
padding: 0.25rem;
8787
}
88+
89+
.category-list > .category-item:hover > .category-list {
90+
display: flex;
91+
}
92+
93+
.category-list > .category-item > .category-list > .category-item > .category-list {
94+
left: 100%;
95+
top: 0;
96+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@props(['categoryList'])
2+
3+
<div {{ $attributes->merge(['class' => 'category-list flex text-white bg-slate-700']) }}>
4+
@if (!empty($categoryList))
5+
@foreach($categoryList as $category)
6+
<div class="category-item relative">
7+
<a href="{{ route('byCategory', $category) }}" class="cursor-pointer block py-3 px-6 hover:bg-black/10">
8+
{{$category->name}}
9+
</a>
10+
<x-category-list class="absolute left-0 top-[100%] z-50 hidden flex-col" :category-list="$category->children"/>
11+
</div>
12+
@endforeach
13+
@endif
14+
</div>

resources/views/product/index.blade.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
22
/** @var \Illuminate\Database\Eloquent\Collection $products */
3+
$categoryList = \App\Models\Category::getActiveAsTree();
4+
35
?>
46

57
<x-app-layout>
8+
<x-category-list :category-list="$categoryList" class="-ml-5 -mt-5 -mr-5 px-4"/>
69
<?php if ($products->count() === 0): ?>
710
<div class="text-center text-gray-600 py-16 text-xl">
811
There are no products published

routes/web.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
Route::middleware(['guestOrVerified'])->group(function () {
2222
Route::get('/', [ProductController::class, 'index'])->name('home');
23+
Route::get('/category/{category:slug}', [ProductController::class, 'byCategory'])->name('byCategory');
2324
Route::get('/product/{product:slug}', [ProductController::class, 'view'])->name('product.view');
2425

2526
Route::prefix('/cart')->name('cart.')->group(function () {

0 commit comments

Comments
 (0)