Dylan Pierce
Dylan Pierce

Reputation: 4668

Laravel - How to query a chain of one-to-many relationship to one-to-one relationship

The end goal is to get a collection of Albums from a User. However there is another model between them: Albums.

$user->artists()->get(); //this will retrieve a collection of artists from a particular user

$user->artists()->albums()->get() //results in: Call to undefined method Illuminate\Database\Query\Builder::riderNegotiation()

My models aren't actually called this but for simplicities sake this is what I'm calling them. So changing the database structure is not an option.

Upvotes: 3

Views: 1486

Answers (1)

user2920996
user2920996

Reputation:

You can query relations and data will be injected in $user instance:

// if you already have an instance
$user->load('artists.albums'); 

or

// get a user instance with related data 
$user = User::with('artists.albums')->find($id); 

Upvotes: 2

Related Questions