Solution:
If a User can have many heroes, and a Hero can also belong to many users, it is a many to many relationship. In Laravel the inverse of a many to many relationship is also a many to many relationship, and they are both described by belongsToMany()
.
https://laravel.com/docs/5.2/eloquent-relationships#many-to-many
So in your User model:
public function heros() {
return $this->belongsToMany(Hero::class);
}
And in your Hero model:
public function users() {
return $this->belongsToMany(User::class);
}
Laravel will assume the joining table is named hero_user
, the 2 model names, singular, joined in alphabetical order. If you want to use user_heroes
as you have in your image, you need to specify it:
return $this->belongsToMany(Hero::class, 'user_heroes');
(in both model methods).