Skip to content

Instantly share code, notes, and snippets.

@JonoB
Last active December 20, 2015 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonoB/6130478 to your computer and use it in GitHub Desktop.
Save JonoB/6130478 to your computer and use it in GitHub Desktop.
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