Last active
December 20, 2015 12:19
-
-
Save JonoB/6130478 to your computer and use it in GitHub Desktop.
Eager loading relationships in a repository
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If we have a model that has a lot of relationships, we need a method to be able to dynamically | |
// eager-load those relationships in an easy way | |
// For example, some controller methods may only require one relationship to be loaded | |
// Others may require all relationships to be eager loaded | |
// Repository | |
class UserRepository implements UserInterface | |
{ | |
public function find($id, $relations = array()) | |
{ | |
return Song::with($relations)->findOrFail($id); | |
} | |
} | |
// Controller | |
// just load the user | |
$user = $this->users->find(1); | |
// Load up some relationships | |
$this->users->find(1, array('groups', 'status', 'books')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment