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

Hi,

I've been looking at Cache docs: http://laravel.com/docs/5.0/cache

I want to use Cache in my models, so I was thinking like this:

class Question extends Eloquent
{
    ...
    public function scopeGetNewest($query)
    {
        if (Cache::has('newest'))
        {
            return Cache::get('newest')
        }
        
        $newest = $query->where(...)->get();
        Cache::put('newest', $newest, 120); // 2 hrs
        return $newest;
    }
}

..but this isn't going to let me mock the Cache object in tests :/

How should I do this? Should I pass the cache object in with the models constructor:

public function __construct($cache)
{
   $this->cache = $cache;
}

Is there anyway to handle this via IoC?

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

martynbiz martynbiz Joined 15 Feb 2015

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.