Uhmm, I think you are looking for this:
https://laravel.com/docs/8.x/eloquent-relationships#has-many-through
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Let's say I have 3 models. Doctor, Area and Area_subspecialty.
My tables.
public function up() {
Schema::create('doctors', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->foreignId('area_id')->constrained();
$table->string('colmedico')->nullable()->unique();
$table->string('abreviatura');
});
}
public function up() {
Schema::create('areas', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('nomarea')->unique();
$table->foreignId('departamento_id')->constrained();
});
}
public function up() {
Schema::create('area_especials', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->foreignId('area_id')->constrained();
$table->string('nombespeciali')->unique();
});
}
My Models
class Doctor extends Model {
public function cualespeci(){
return $this->hasOne(Area::class, 'id','area_id');
}
}
class Area extends Model {
public function doctoarea(){
return $this->belongsTo(Doctor::class,'area_id');
}
class AreaEspecial extends Model {
public function larea() {
return $this->belongsTo(Area::class, 'area_id');
}
A doctor belongs to an area, but a doctor can be assigned to several sub_areas, my problem is that I don't know how to get the areas_subspecialties that belong to a doctor, can someone please help me?
if I look for a Doctor. Example $d= Doctor::find(1); I get the data of the doctor and if I want to see which area he belongs to I do $d->cualespeci and I get the area to which he belongs now I just have to know which areas_subspecialties he belongs to.
sorry for my English
Please or to participate in this conversation.