Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Eager loading relationships in a repository
// 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