Home > Back-end >  Map Laravel multiple collection
Map Laravel multiple collection

Time:06-16

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?

CodePudding user response:

To access to this properties. You should do a loop with for-each, but before you should send this data to blade or send blade property that you need. in your blade make this:

foreach($departaments as $departament){
     <p>{{ $departament->provinces->name }}</p>
}

for example

CodePudding user response:

From I understand from your question and also referring to the documentation, your database table should be like this:

provinces
    id
    name

districts
    id
    name
    province_id

departments
    id
    name
    district_id

My suggestion is to not use the pivot-based or has many through, considering on having a better way on displaying your data in blade, use one to many pattern should be more suitable.

For Department:

class Department extends Model
{
    /**
     * Get the district for the department.
     */
    public function district()
    {
        return $this->belongTo(District::class);
    }
}

For District:

class District extends Model
{
    /**
     * Get the departments for the district.
     */
    public function departments()
    {
        return $this->hasMany(Department::class);
    }

    /**
     * Get the provinces for the district.
     */
    public function provinces()
    {
        return $this->belongTo(Province::class);
    }
}

For Province:

class Province extends Model
{
    /**
     * Get the districts for the provinces.
     */
    public function districts()
    {
        return $this->hasMany(District::class);
    }
}

The pattern I suggested here cannot access some patterns such as accessing all provinces with same departments, but can easily define in your controller or your model, but it should be more logical when trying to access them through blade, such as:

<div>
    @foreach($departments as $department)
        <p>Department Name: {{ $department->name }}</P>
        <p>Department Province: {{ $department->province->name }}</P>
        <p>Department District: {{ $department->province->district->name }}</P>
    @endforeach
</div

If you are trying to access provinces' department, $province->departments will return an array of departments which is under the same provinces, and you can use the same way to access the data through blade as well.

So DPD table is not necessary at this point, hope this help you.

  • Related