Laravel table relationship to pivot table

Robert Tirta :

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

Robert Tirta :

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

Tried at tinker and it works,

First, 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, 'location_area_type_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',
        'location_area_type_id');
}

this way I can get the areaTests easily by $location->areaTests, My problem was not determining the location_area_type_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.

This way, I can also access it through the pivot table by

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

But, 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 (since it is the foreign determined location_area_type_id

So in the Location model I have to get the column

public function poolTypes()
{
    // Get the ID of the pivot table to get the poolTests table (connected with ID column)
    return $this->belongsToMany(PoolType::class)
        ->using(LocationPoolType::class)
        ->withPivot('id');
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=19839&siteId=1