I have three tables (Departments, Provinces, and Districts), they have many-to-many relationships with each other, and their relationship through the "DPD" table. when I want to access a single department with its all districts and provinces (I mean that this department is located in which districts of which provinces). here is the relationship between them
public function districts(){
return $this->hasManyThrough(District::class,DPD::class,'district_id','id','id','department_id');
}
public function provinces(){
return $this->hasManyThrough(Province::class,DPD::class,'province_id','id','id','department_id');
}
when I do this
Department:: with('provinces','districts')->get()
it returns multidimentional collection of department with multiple of provinces and districts. like
Illuminate\Database\Eloquent\Collection {#4595
all: [
App\Models\Department{#4605
id: 1,
name: "ریاست مخابره",
created_at: null,
updated_at: null,
provinces: Illuminate\Database\Eloquent\Collection {#4623
all: [
App\Models\Province {#4580
id: 1,
name: "کابل",
created_at: null,
updated_at: null,
laravel_through_key: 1,
},
App\Models\Province {#4630
id: 1,
name: "کابل",
created_at: null,
updated_at: null,
laravel_through_key: 1,
},
],
},
},
App\Models\Department{#4613
id: 2,
name: "ریاست لوجیستیک",
created_at: null,
updated_at: null,
provinces: Illuminate\Database\Eloquent\Collection {#4566
all: [
App\Models\Province {#4617
id: 2,
name: "ننګرهار",
created_at: null,
updated_at: null,
laravel_through_key: 2,
},
App\Models\Province {#4596
id: 1,
name: "کابل",
created_at: null,
updated_at: null,
laravel_through_key: 2,
},
App\Models\Province {#4629
id: 2,
name: "ننګرهار",
created_at: null,
updated_at: null,
laravel_through_key: 2,
},
],
},
},
],
} now I want to access every department with their own province and district for easy displaying, I had problems with it when I want to display in the blade, like when I display a department then I can only display only district or province, cant display both of them. is there any solution?