37 lines
1.3 KiB
PHP
37 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Category;
|
|
use App\Models\Product;
|
|
|
|
class HomepageController extends Controller
|
|
{
|
|
public function __invoke()
|
|
{
|
|
$search = request('search');
|
|
$categorySlug = request('category');
|
|
|
|
return inertia('homepage', [
|
|
'categories' => Category::active()->get(),
|
|
'bestSellers' => Product::with(['categories' => fn ($q) => $q->active(), 'prices'])
|
|
->when($search, function ($query, $search) {
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('name', 'like', "%{$search}%")
|
|
->orWhere('description', 'like', "%{$search}%");
|
|
});
|
|
})
|
|
->when($categorySlug, function ($query, $categorySlug) {
|
|
$query->whereHas('categories', function ($q) use ($categorySlug) {
|
|
$q->where('categories.slug', $categorySlug);
|
|
});
|
|
})
|
|
->active()
|
|
->latest()
|
|
->paginate(12)
|
|
->withQueryString(),
|
|
'topSellingProducts' => Product::with(['categories' => fn ($q) => $q->active(), 'prices'])->withCount('orderItems')->active()->orderBy('order_items_count', 'desc')->take(3)->get(),
|
|
]);
|
|
}
|
|
}
|