muuucho's avatar
Level 11

belongsToMany with other id names

May be not an Eloquent question. It is about relations. I have this tables: team: id, name modules: id, name module_team: id, team_id, module

Relations: A Team belongsToMany Module A Module belongsToMany Team

my related ids are: teams.id = module_team.id (=meets Laravel conventions) modules.name = module_team.module (= does not meet Laravel naming conventions, but will prevent use of belongsToMany relation?)

If I check the belongToMany method in the Laravel file HasRelationships.php, I can see that it accepts 7 arguments of which the 6 first are of interest in my case. I try to put my keys in the right positions here. I start with Model Team: $related = ModuleTeam::class, $table = 'module_team', $foreignPivotKey = ?, $relatedPivotKey = ?, $parentKey = ? $relatedKey = ?

Now the Model Module: $related = ModuleTeam::class, $table = 'module_team', $foreignPivotKey = ?, $relatedPivotKey = ?, $parentKey = ? $relatedKey = ?

Could someone please replace the questionmarks with correct values?

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To set up a belongsToMany relationship in Laravel when your pivot table does not follow the default naming conventions, you need to specify the custom column names in the belongsToMany method. Here's how you can define the relationships for your Team and Module models:

Team Model

For the Team model, you want to define a belongsToMany relationship with the Module model. The pivot table is module_team, and the relevant columns are team_id and module (which corresponds to modules.name).

public function modules()
{
    return $this->belongsToMany(Module::class, 'module_team', 'team_id', 'module', 'id', 'name');
}

Module Model

For the Module model, you want to define a belongsToMany relationship with the Team model. The pivot table is the same, and the relevant columns are module (which corresponds to modules.name) and team_id.

public function teams()
{
    return $this->belongsToMany(Team::class, 'module_team', 'module', 'team_id', 'name', 'id');
}

Explanation of Parameters

  • $related: The related model class (Module::class or Team::class).
  • $table: The name of the pivot table (module_team).
  • $foreignPivotKey: The foreign key on the pivot table that references the current model (team_id for Team, module for Module).
  • $relatedPivotKey: The foreign key on the pivot table that references the related model (module for Team, team_id for Module).
  • $parentKey: The primary key of the current model (id for Team, name for Module).
  • $relatedKey: The primary key of the related model (name for Team, id for Module).

By specifying these parameters, you can successfully set up the belongsToMany relationship even when your database schema does not follow Laravel's default conventions.

1 like
muuucho's avatar
Level 11

I find Laravels argument names confusing. I name them this instead: BelongsToMany(Model::class, 'pivotTable', 'pivotThisKey', 'pivotOtherKey', 'thisKey', 'foreignOtherKey') This way you don't have to come up with explanations like "$relatedPivotKey: The foreign key on the pivot table that references the related model"

Please or to participate in this conversation.