Caching relationships eager loaded in the model (protected $with) with Laravel and Eloquent

Thomas Jensen

In my Article model I eager load the category relationship within the model:

protected $with = array('category');

When I get my articles in the controller I cache the results of both my articles and the relationships I 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 does not get cached. I thought it would get cached together with the Article model, since it is eager loaded in that model. But this does not happen.

It does however get cached if I eager load (with cache) in the controller by adding this to my Article::with:

'category' => function ($q) {$q->remember(10);}

I know that I can remember() the relationship itself:

public function category()
{
    return $this->belongsTo('Category', 'category_id')->remember(10);
}

But then it will always be cached, I would like to define whether and for how long it is cached in the controller. As the values may vary with use.

Is that possible? Or must the caching be defined in the model, since the eager loading happens there?

Jarek Tkaczyk

Just use with('category' => ... in your controller.

You might want to extract this to a repository though, so you don't stuff your controller with all that logic. And repositories are trendy nowadays ;)

Search for repository pattern to learn more.

In laravel world repository mostly refers to an abstraction over the storage layer (be it db and eloquent or anything else), that will decouple your controllers from the persistence engine you're using.

And what's more important, it allows easier testing of your code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel Eloquent: eager loading of multiple nested relationships

From Dev

Laravel - eager loading a method (not a relationship) of an Eloquent model

From Dev

Laravel - Multiple query function to multiple eager loaded relationships

From Dev

Laravel eloquent: Update A Model And its Relationships

From Dev

Laravel dynamic relationships - access model attributes on eager load

From Dev

Laravel Eloquent - Eager loading

From Dev

Laravel / Eloquent eager loading

From Dev

Laravel Eloquent - Eager loading

From Dev

Laravel Eager Loading Polymorphic Relationships

From Dev

Laravel Eager Loading Polymorphic Relationships

From Dev

Laravel eager relationships empty query

From Dev

Eager load multiple relationships in Laravel

From Dev

Laravel5: How are Eloquent model relationships expressed in the database?

From Dev

Laravel Caching the Eloquent

From Dev

Laravel Eloquent querying relationships

From Dev

Laravel Eloquent Relationships

From Dev

Eager loading inside eager loading in Laravel Eloquent

From Dev

conditions in eager loading in eloquent laravel

From Dev

Laravel Eloquent Eager loading confusion

From Dev

Caching Lazy Eager Loading Queries in Laravel 5.1

From Dev

Laravel 4.1 Eager Loading Nested Relationships with Constraints

From Dev

Laravel eager loading nested relationships with custom query

From Dev

Eloquent (without Laravel) caching implementation

From Dev

Laravel eloquent relationships with foreign keys

From Dev

Laravel Eloquent Case Sensitive Relationships

From Dev

Laravel / Eloquent relationships & querying the database

From Dev

Laravel eloquent complex queries on relationships

From Dev

Laravel 5.2 Nested Eloquent relationships

From Dev

Drill down into laravel eloquent relationships

Related Related

  1. 1

    Laravel Eloquent: eager loading of multiple nested relationships

  2. 2

    Laravel - eager loading a method (not a relationship) of an Eloquent model

  3. 3

    Laravel - Multiple query function to multiple eager loaded relationships

  4. 4

    Laravel eloquent: Update A Model And its Relationships

  5. 5

    Laravel dynamic relationships - access model attributes on eager load

  6. 6

    Laravel Eloquent - Eager loading

  7. 7

    Laravel / Eloquent eager loading

  8. 8

    Laravel Eloquent - Eager loading

  9. 9

    Laravel Eager Loading Polymorphic Relationships

  10. 10

    Laravel Eager Loading Polymorphic Relationships

  11. 11

    Laravel eager relationships empty query

  12. 12

    Eager load multiple relationships in Laravel

  13. 13

    Laravel5: How are Eloquent model relationships expressed in the database?

  14. 14

    Laravel Caching the Eloquent

  15. 15

    Laravel Eloquent querying relationships

  16. 16

    Laravel Eloquent Relationships

  17. 17

    Eager loading inside eager loading in Laravel Eloquent

  18. 18

    conditions in eager loading in eloquent laravel

  19. 19

    Laravel Eloquent Eager loading confusion

  20. 20

    Caching Lazy Eager Loading Queries in Laravel 5.1

  21. 21

    Laravel 4.1 Eager Loading Nested Relationships with Constraints

  22. 22

    Laravel eager loading nested relationships with custom query

  23. 23

    Eloquent (without Laravel) caching implementation

  24. 24

    Laravel eloquent relationships with foreign keys

  25. 25

    Laravel Eloquent Case Sensitive Relationships

  26. 26

    Laravel / Eloquent relationships & querying the database

  27. 27

    Laravel eloquent complex queries on relationships

  28. 28

    Laravel 5.2 Nested Eloquent relationships

  29. 29

    Drill down into laravel eloquent relationships

HotTag

Archive