[Vuejs]-How do I define relationships between User, Post and Like models

0👍

I don’t think you need Like model, because you can create direct relation between User and Post, but with different table name, e.g. “likes”.

User model:

public function likes()
{
    return $this->belongsToMany(Post::class, "likes");
}

Post model:

public function likes()
{
    return $this->belongsToMany(User::class, "likes");
}

More about Many to Many relationship: https://laravel.com/docs/5.5/eloquent-relationships#many-to-many

Leave a comment