Skip to content
Advertisement

Laravel table relationship to pivot table

I’m confused on how to get my model setup in laravel with a table that is connected to a pivot table.

Here’s the problem

Say I have

Locations
    id
    name

area_types
    id
    name

area_type_location (Pivot table)
    id
    location_id
    area_type_id

area_test
    id
    area_type_location_id
    clean
    headCount

Relationship between the tables are different areas type belongs to different locations. i.e: beach, 25m pools, kids pools, bbq, etc

area_test is connected to the pivot table because the test has to be generated from area that exists, in this case it is area that is registered under different locations. Thus it has to be tested daily, measured, etc.

I understand the structure between area_types and locations for many to many relationship, however I can’t get over my head of how do i structure my area_test model? How do I get the data from locations table -> where are my test?

Should I create a model for my pivot table? Is that a good practice in laravel?

Does anyone has the same use case?

I read about eloquent has many through relationship but I understand that it does not mention about getting through pivot table. I don’t quite get if my use case is the same.

Thanks

Advertisement

Answer

Finally, apparently there are a couple of way to get data from locations table to area_tests

Tried at tinker and it works,


First Option

I need to create a Pivot model for my Pivot table:

class LocationAreaType extends Pivot{

public function location(){
    return $this->belongsTo(Location::class);
}

public function areaType(){
    return $this->belongsTo(AreaType::class);
}

public function AreaTests(){
    return $this->hasMany(AreaTest::class, 'area_type_location_id');
}

}

I can use hasManyThrough relation that I need to create in my Location table

public function areaTests()
{
    return $this->hasManyThrough(
        AreaTest::class,
        LocationAreaType::class,
        'location_id',
        'area_type_location_id');
}

this way I can get the areaTests easily by $location->areaTests, My problem was not determining the area_type_location_id as foreign. You need to determine this, apparently when I extends pivot and use hasMany laravel does not auto recognise the Foreign key by itself.


Second option

Another way to access it is from the relation table, I can define withPivot in the areaTypes() relation then access it like this:

$location->areaType[0]->pivot->areaTests

Since laravel only recognise foreign key from both tables location_id and area_type_id, I have to include the id of the pivot table to get the AreaTest table data

So in the Location model I have to get the column

public function areaTypes()
{
    // Get the ID of the pivot table to get the poolTests table (connected with ID column)
    return $this->belongsToMany(AreaType::class)
        ->using(AreaTypeLocation::class)
        ->withPivot('id');
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement