2026-08-26 13:43:30 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
2026-08-26 18:28:16 +03:00
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
2026-08-26 13:43:30 +03:00
|
|
|
use Lunar\Models\Collection;
|
2026-08-26 18:28:16 +03:00
|
|
|
use Modules\Core\Catalog\ProductFilters;
|
|
|
|
|
use Modules\Core\Catalog\ProductService;
|
2026-08-26 13:43:30 +03:00
|
|
|
|
|
|
|
|
class CategoryController extends Controller
|
|
|
|
|
{
|
2026-08-26 18:28:16 +03:00
|
|
|
public function __construct(
|
|
|
|
|
private readonly ProductService $products,
|
|
|
|
|
) {}
|
|
|
|
|
|
2026-08-26 13:43:30 +03:00
|
|
|
public function show(string $locale, Collection $collection)
|
|
|
|
|
{
|
2026-08-26 18:28:16 +03:00
|
|
|
$perPage = 12;
|
|
|
|
|
$page = (int) request('page', 1);
|
|
|
|
|
|
|
|
|
|
// Listing/filtering reads from the Meilisearch index via ProductService,
|
|
|
|
|
// not Eloquent — see Modules\Core\Catalog\ProductService. It returns plain
|
|
|
|
|
// arrays (already localized/flattened), not Product models.
|
|
|
|
|
$result = $this->products->list(
|
|
|
|
|
filters: new ProductFilters(collectionId: $collection->id),
|
|
|
|
|
perPage: $perPage,
|
|
|
|
|
page: $page,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$products = new LengthAwarePaginator(
|
|
|
|
|
items: collect($result['data'])->map(fn (array $product) => [
|
|
|
|
|
'name' => $product['name'],
|
|
|
|
|
'price' => $product['price'],
|
|
|
|
|
'image' => $product['media'][0]['url'] ?? null,
|
|
|
|
|
'href' => route('product.show', ['product' => $product['id']]),
|
|
|
|
|
]),
|
|
|
|
|
total: $result['meta']['total'],
|
|
|
|
|
perPage: $result['meta']['per_page'],
|
|
|
|
|
currentPage: $result['meta']['current_page'],
|
|
|
|
|
options: [
|
|
|
|
|
'path' => request()->url(),
|
|
|
|
|
'query' => request()->query(),
|
|
|
|
|
],
|
|
|
|
|
);
|
2026-08-26 13:43:30 +03:00
|
|
|
|
|
|
|
|
return view('category.show', [
|
|
|
|
|
'collection' => $collection,
|
|
|
|
|
'products' => $products,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|