fightstarr20
fightstarr20

Reputation: 12598

Laravel 5.5 - Return hasMany results from another hasMany

I am getting all memberships in a Laravel 5.5 API route like this...

$membership= Membership::with('user')->findOrFail($id);

return Response::json(array(
        'error' => false,
        'user_data' => $membership,
    ));

I have a Membership hasMany Users relationship setup so this correctly returns all users belonging to this membership.

I also have a Users hasMany Images relationship setup, is there a way I can also return a list of all images that belong to the returned users?

Should I be creating a seperate function that compiles a list or can it be done directly from the with statement?

Upvotes: 0

Views: 49

Answers (2)

Nikola Gavric
Nikola Gavric

Reputation: 3543

If by Subscription you meant Membership then the code below should work

$membership= Membership::with('user.images')->findOrFail($id);

Upvotes: 1

Mahdi Younesi
Mahdi Younesi

Reputation: 7489

Try this, No need for extra relation load

$membership= Membership::with( 'user.images')->findOrFail($id);

//get users of this membership  with their images

Upvotes: 1

Related Questions