I am fairly new to Laravel and I have been trying to find the best way to create a cache wrapper for all models.
For example, when I search for a user by email, I want to do something like User->getByEmail('test@exmaple.com');
My model would contain a function that looks something like
public static function getByEmail($email) {
$cache = self::cacheFetch($email);
if($cache) {
return $cache;
}
// DB lookup to get data
self::cacheStore($email, $data);
return $data;
}
cacheFetch
and cacheStore
are defined as traits. The App name and table name are prepended to the cache key to avoid key clash.
How do I do the database lookup within the Model?
Is there a better way to achieve what I am trying to do here?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community