I have a Post model:
$posts = Post::with('comments')->get();
and every comment has user_id row
I want to get the user with every comment, with relations.
My comments method in Post model
public function comments()
{
return $this->hasMany(Comment::class);
}
what is the best approach to get the user with comment?
final result:
{
'id': 1,
'title': 'some title',
'caption': 'some caption:,
'comments': [
{
'id': 1,
'text': 'some text',
'user_id': 1,
'user': { 'id':1, 'username': 'something', 'email': 'some@email.com }
},
{
'id': 2,
'text': 'some text',
'user_id': 2,
'user': { 'id':2, 'username': 'something', 'email': 'some2@email.com }
},
]
}