Reputation: 1871
Right now I've got this in my Settings.php
model:
public function scopeFromCache($query)
{
\Cache::rememberForever('settings', function () use ($query) {
return $query->first();
})->first();
}
Then in the boot
method from my AppServiceProvider
I do this:
$settings = Settings::fromCache()->first();
Is it possible to get the settings without the ->first()
like this:
$settings = Settings::fromCache();
So instead of returning a query builder
return the object?
Upvotes: 0
Views: 423
Reputation: 8385
To answer your question, no.
I would take another approach, since you are caching something forever (settings), I would make custom helper to get you these settings.
So create file app/helpers.php, in composer.json add
"files": [
"app/helpers.php"
]
in "autoload" array.
Now forget about using scope, and create method (function) to use cache, just like you are doing now:
if ( ! function_exists('settings')) {
function settings()
{
return Cache::rememberForever('settings', function () {
return Settings::first();
});
}
}
Now anywhere in your project just call settings()
and you get your object.
Upvotes: 1