Home > Mobile >  How dows eager loading work for relations?
How dows eager loading work for relations?

Time:10-18

I have read the docs I got that if call relation like: users()->author->name it sends another one request in db each time.

But if to call using User()::with("author") it will be loaded eager.

Therefore I try to understand how does it work if I call sometimes the method Auth::user() in controller:

public function controllerMethod(){
  Auth::user()->tags();
  /// Some logic 

  Auth::user()->id;
  // Some logic
 
  Auth::user()->tags();
} 

Does it create a three the same requets in db or two the same and one additional with tags?

If yeas how toad it in global score because I need to get Auth::user() object everywehere in app to show the users fields.

CodePudding user response:

User()::with() is actually an error:

Call to undefined function User()

The correct syntax would be User::with()

  • Related