Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

alanreed's avatar

Caching entire model always

I have a model that never changes so I want to cache it. We access the data in a lot of different files and I don't want to repeat Cache::rememberForever(...) all over the place. So I centralized it in the model inside a function called allCached().

Here is my function in the model:

public static function allCached() {
    return \Cache::rememberForever('categories', function() {
        return Category::all();
    });
}

Then I call it in multiple controllers like this: $categories = Catagory::allCached();

I get the feeling this is a bad way to do it. I'm not really sure how to improve this. What is a better way of doing this?

0 likes
8 replies
Snapey's avatar

I would consider only caching the attributes you are likely to need

1 like
alanreed's avatar

@Snapey

We need all the columns in this case. Either way it is easy to pass through the list of necessary columns.

I still feel like this is a sloppy way of doing it. Like there should be a more "Laravel" way to solve this.

Am I wrong about that? Is this the best approach?

tisuchi's avatar

@alanreed I agree with you. It's not a good approach to touch the default model.

Instead what I suggest you to use the boot() method in the Category model.

class Category extends Model
{
    public static function boot()
    {
        parent::boot();

        static::retrieved(function ($model) {
            Cache::rememberForever('categories', function() use ($model) {
                return $model;
            });
        });
    }
}
alanreed's avatar

@tisuchi our allCached() function is not in the default model. It is in the Category model.

tisuchi's avatar

@alanreed Maybe my understanding gap. Apology for that. But this line indicates that to me.

So I centralized it in the model inside a function called allCached().

alanreed's avatar

@tisuchi I see. That is ambiguous.

The allCached() function is inside the Category model. Is that the right approach? Maybe the function is just named poorly?

tisuchi's avatar

@alanreed If you're in search of an optimized solution, I highly recommend following @snapey's suggestion. I also believe it's the right way to do that.

However, if you still choose to proceed with caching the entire model, then it would be wise to at least set a specific caching time frame.

azimidev's avatar

I agree with @snapey but this is how you can do that. I would recommend caching the attrs.

override the all() method in the model using cache rememberforever:

public static function all() {
    return static::cacheForever('categories', function() {
        return Category::all();
    });
}

public static function cacheForever($key, $callback) {
    return \Cache::rememberForever($key, $callback);
}

This way, if you need to change the caching logic in the future, you only need to update the cacheForever method, instead of having to change it in multiple places.

Please or to participate in this conversation.