Looks like a belongsToMany relation is what you need.
//The Profile model
public function specialties()
{
return $this->belongsToMany(Speciality::class)->withPivot('level');
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
So I got the following table structure:
Now I thought I would need a query like:
//Inside the controller
dd(Profile::with('specialties'));
//The Profile model
public function specialties()
{
return $this->hasManyThrough(Profile_has_specialty::class, Specialty::class, 'id', 'profiles_id');
}
But this returns nothing. The specialties remains empty. How would I achieve that I can get all specialties for a given user with Eloquent?
Ah, yes. I see you have a non-conventional pivot table name, so you need to specify this as the second argument:
//The Profile model
public function specialties()
{
return $this->belongsToMany(Speciality::class, 'profiles_has_specialties')->withPivot('level');
}
Sorry I missed that the first time!
Please or to participate in this conversation.