I have a model called Product and a model called Price.
Each product has its own price, which is not related to the Price model. The Price model is an independent set of data managed elsewhere.
In my Price table I have a list of original prices alongside a replacement price, for example:
Original Price New Price
100 250
500 599
1000 1200
When I fetch a Product, let's call it "Widget", if its price is one of $100, $500 or $1000 then I want to show the corresponding New Price from the Price table. If the product's price is something else, for example $50, then I want to just show the original value.
In my Product model I have:
public function getPriceAttribute($price)
{
$prices = Price::pluck('new_price', 'original_price')->toArray();
return $prices[ $price ] ?? $price;
}
This work okay, but I am wanting to cache the price data so that I don't have to query the Price table every single time I display a product price.
Caching in Laravel is generally for a set amount of time. I wondered if it was possible to cache this data just for a single request. Or whether I can aquire the data from outside of the model a single time and somehow inject it, so that my Product model isn't intrinsically reliant on the Price model.
Thanks