r/laravel•
Crychair

New to laravel. Question about eloquent relationships

So I have 3 tables and I am trying to do a Many to Many.

Players
- bb1_player_id

Skills
- bb1_skill_id

Player_skills
- bb1_player_id
- bb1_skill_id

When I do the normal Many to Many it automatically uses the id column from my Player and Skills tables. It uses the correct columns on the Player_skills table though.

My model method:

public function skills()
{
        return $this->belongsToMany('App\Skills', 'player_skills', 'bb1_player_id', 'bb1_skill_id');
    }

The last two arguments of that seem to do nothing. But I figured I try it.

I get this query from that:

"select `skills`.*, `player_skills`.`bb1_player_id` as `pivot_bb1_player_id`, `player_skills`.`bb1_skill_id` as `pivot_bb1_skill_id` from `skills` inner join `player_skills` on `skills`.`id` = `player_skills`.`bb1_skill_id` where `player_skills`.`bb1_player_id` = ?"

And if you look at that query it is using the skills.id and where the '?' is it is using the player.id. Is there a way to override this? Or work around it without catering my database tables to it?

I already wasted some time making a player_skills table to try to use this feature. I might be at the point where redesigning my database might be the option unless this can be solved.

Posted to Stack Overflow as well:

http://stackoverflow.com/questions/39216376/laravel-many-to-many-without-using-id-column

Thanks for the help.

Posted Aug 29, 2016, 11:18 PM UTC

525
Loading comments…