haizone
100
0
Laravel

how to laravel cache resources?

Posted 4 months ago by haizone

i have this list that i get from DB

$products = $category
                    ->products()
                    ->with(['reviews','businessProducts','productimage','manufacturer'])
                    ->orderBy('position')
                    ->paginate(2);
return ProductIndexResource::collection($products);

now i get a collection of products for that category

how i can cache every product in the products collection that i get from the category products? somthing like that, with uniq if from every product

$product = Cache::remember('product-'.$product->id, 14400, function() use ($products){
        return ???;
     });

this is my resources

    public function toArray($request)
{
    return [
        'name' => trim($this->name),
        'english_name' => $this->english_name,
        'slug' => $this->slugi,
        'model' => $this->model,
        'position' =>$this->position,
        'details' =>$this->details,
        'productoptions' =>$this->productoptions,
    ];
}

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.