M
M
moscowthebest2019-05-01 14:07:07
Laravel
moscowthebest, 2019-05-01 14:07:07

How to get data from related models?

Hey!
There is a table " Posts "
- id
- body
- created_at
- updated_at

Table " Comments "
- id
- post_id
- user_id
- body
- created_at
- updated_at

Table " Relies " ( Answers under comments )
- id
- comment_id
- user_id
- body
- replied_to
- created_at
- updated_at

I want : Return a post ( Post ) with comments, replies and users whose id was specified .
I used to . connected them like this:$post::with(['comments.user'])->first();
comments and user are methods written inside the model.
Help me solve the problem please :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Wells, 2019-05-01
@moscowthebest

Post::query()
    ->with('comments.user')
    ->with('comments.replies.user')
    ->findOrFail($id);

In general, redo the structure as I told you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question