0

I have 3 related models. 1.User

public function book(){
        return $this->hasMany('App\Book');
    }

2.Book

public function photo(){
        return $this->hasMany('App\Books_photo');
    }

    public function user(){
        return $this->belongsTo('App\User');
    }

3.Books_photo

public function book(){
        return $this->belongsTo('App\Book');
    }

The relations are

User hasmany Book

and

Book hasmany Books_photo

So I want to get all the books with their photo of a particular user.

I can get all the books of a user.I'm using this approach

$User = User::with('book')->find(decrypt($request->id));

But this returns only the data in book and user table. How can i fetch the photos too.

1 Answer 1

1

You're looking for nested eager loading. You will want to do this:

$User = User::with('book', 'book.photo')->find(decrypt($request->id));

The book.photo is a nested eager load that will tell eloquent to get the photo relation from the book model.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.