Support the ongoing development of Laravel.io →
Cache Eloquent Laravel

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?

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Naveen naveenkeloth Joined 14 Dec 2018

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.