V
V
Vitaly2018-05-25 18:02:57
Laravel
Vitaly, 2018-05-25 18:02:57

Laravel. How to attach user to comments?

there are 3 tables

posts
       id,name
comments
       id, post_id, user_id
user
       id,name,image

This is how I get the Post This is how I get all the comments
$post = Post::where("alias", $alias)->first();
$comments = $post->comments;
public function comments()
    {
        return $this->hasMany('App\Post', 'post_id');
    }

You need to select all the comments of the post and attach usera to each comment to get their photos and names.
How to make the right choice.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pLavrenov, 2018-06-04
@pLavrenov

Relationships - https://laravel.ru/docs/v5/eloquent-relationships
From comments to user - hasOne and with()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question