Answer the question
In order to leave comments, you need to log in
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
Post::query()
->with('comments.user')
->with('comments.replies.user')
->findOrFail($id);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question