php - Caching relationships eager loaded in the model (protected $with) with Laravel and Eloquent -
php - Caching relationships eager loaded in the model (protected $with) with Laravel and Eloquent -
in article
model eager load category relationship within model:
protected $with = array('category');
when articles in controller cache results of both articles , relationships eager load there:
$articles = article::with([ 'owner' => function ($q) {$q->remember(10);}, 'tags' => function ($q) {$q->remember(10);}, 'thumbnail' => function ($q) {$q->remember(10);} ]) ->remember(10)->get();
but; article.category
not cached. thought cached article
model, since eager loaded in model. not happen.
it cached if eager load (with cache) in controller adding article::with
:
'category' => function ($q) {$q->remember(10);}
i know can remember()
relationship itself:
public function category() { homecoming $this->belongsto('category', 'category_id')->remember(10); }
but cached, define whether , how long cached in controller. values may vary use.
is possible? or must caching defined in model, since eager loading happens there?
just utilize with('category' => ...
in controller.
you might want extract repository though, don't stuff controller logic. , repositories trendy nowadays ;)
search repository pattern larn more.
in laravel world repository refers abstraction on storage layer (be db , eloquent or else), decouple controllers persistence engine you're using.
and what's more important, allows easier testing of code.
php caching laravel laravel-4 eloquent
Comments
Post a Comment