/products — what anyone may see, page-paginated. * * ?filter[column]=value one per public filter * ?sort=-created_at leading minus for descending * ?include=relation only a relation whose module is public * ?per_page=15|25|50|100 */ public function index(Request $request): JsonResponse { $perPage = PageSize::api($request); $items = ApiQuery::public(FilteredProducts::publicSet(), PublicApi::includes(Product::class, self::RELATIONS), PublicProductResource::keys()) ->paginate($perPage) ->withQueryString(); return PublicApi::cacheable(PublicProductResource::collection($items)->response()); } /** * A row the scope leaves out, or a key that names nothing, is the same 404. * Cacheable by anyone for a while, then revalidated by its ETag. */ public function show(Request $request, string $key): JsonResponse { $product = $this->visible($key); $includes = PublicApi::load($request, Product::class, self::RELATIONS); return PublicApi::cacheable(Precondition::tagged( (new PublicProductResource($product->load($includes)))->response(), $request, $product, embeds: $includes !== [], )); } /** The record `$key` names, if anyone may see it. */ private function visible(string $key): Product { return Product::query() ->publiclyVisible() ->where('slug', $key) ->firstOrFail(); } }