Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

najeeb-anwari's avatar

Pivot table for three tables

Hello guys, I have three tables; employees, languages, and language_levels. The relationships for those entities are as so;

  • An employee has/knows many languages and a language belongs to many employees.
  • A language has many levels ( beginner, intermediate, fluent, native, ) and a level belongs to many languages.

I want to make a pivot table employee_language_level. My pivot table will have these columns; id, employee_id, language_id, and language_level_id.

  • Should I make a pivot table like this?
  • If yes, how do I make its relationships?
  • how can I use the attach or detach methods in this scenario?
  • If no, what is the better solution?
0 likes
1 reply
RayC's avatar

I would think you only need to setup a pivot table for the employee / languages relationship, add that hasMany relationship to your employee and language models, and add language_id to the language_level table with a hasMany relationship between those two.

Pivot table

employee_language
	employee_id
	language_id

Employee.php Model

public function languages()
{
    return $this->belongsToMany(Language::class);
}

Language.php Model

public function employees()
{
    return $this->belongsToMany(Employee::class);
}

public function languageLevels()
{
    return $this->hasMany(LanguageLevel::class);
}

LanguageLevel.php Model

public function languages()
{
    return $this->hasMany(Language::class);
}

Unless the Language Levels also have a direct relationship with employees.

Please or to participate in this conversation.