Support the ongoing development of Laravel.io →
Cache Eloquent
Last updated 2 years ago.
0

I ended up with this code which I use inside the model. I feel like it can be improved, but it works.

trait TestTrait
{
	public static function fetchByName(string $name)
	{
		$self = new self;

		$value = Cache::remember('test_' . $name, Config::get('cache.ttl'),
        function() use ($name, $self) {
		    return $self::where('name', $name)->first();
		});

		return $value;
	}
}
Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

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.

© 2024 Laravel.io - All rights reserved.