1

I have 2 controllers & Models:

  1. User Controller: (Model Relationship: $this->hasMany(Hero::Class);)

  2. Hero Controller: Each hero has his own attributes, such as name, strength and life. Model Relationship: ($this->belongsTo(User::class);)

Each user can own multiple heroes.

that means that USER ID: 1 may have 3 heroes: HERO ID 5, 20, 26..

My question: How to define the relationships like that and make laravel knows how to handle my user_heroes table? The relationship i'm talking about is described in the following image:

Despired relationship

How to I setup such kind of relationship in my laravel API?

5
  • Yes, I do mean that. Commented Apr 16, 2016 at 14:42
  • 1
    It seems to me what you want is a one-to-many relationship, but your database doesn't reflect that relationship. You need a pivot table in the case of a many-to-many relationship. If you want to have a one-to-many relationship, you need a user_id field in your hero table, you don't need a pivot table. That's how laravel expects you to set up your database for this kind of relationship. Commented Apr 16, 2016 at 14:53
  • Hmm.. when I think about it, ONE user can own multiple heroes but I will still have to use the "many to many" relationship logic. I added an image to be more descriptive of what I'm trying to reach. Can you confrim that although one user can have multiple heroes(which means one to many) I'll still have to use the many to many relationship in laravel? Commented Apr 16, 2016 at 15:03
  • Can more than one user own the same hero? Eg can user id 2 also have hero id 5? Commented Apr 16, 2016 at 15:16
  • Yes, he can also have hero 5. @Don'tPanic Commented Apr 16, 2016 at 15:23

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer. seem like I had to use many to many relationship. Do you know what's the expression to create a new user hero (assuming the user has logged in) in this case?
Something like $user->heros()->attach($hero_id), if the hero already exists. laravel.com/docs/5.2/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.