I have 3 tables
locationables |locations | account_doctor
-----------------+--------------+------------------+
**id** |**id** |**account_id** |
location_id | | |
locationable_id | | |
locationable_type| | |
would you please help me how can i write many to many polymorphic relationship between them?
I have read Laravel documentation but primary key of account_doctor table doesn't have standard conevtion cause of that relationship doesn't retrieve anything
this is my relations:
AccountDoctor model:
public function locations()
{
return $this->morphToMany(Location::class, 'locationable','locationable');
}
Location model:
public function accountDoctors()
{
return $this->morphedByMany(AccountDoctor::class, 'locationable','locationable');
}
The relationship definitions should be as below
class AccountDoctor extends Model
{
public function locations()
{
return $this->morphToMany(
Location::class,
'locationable', //name for the morphable
'locationables', //pivot table
'locationable_id', //foreign key on the pivot table to identify this model record
'location_id', //foreign key on the pivot table to identify related model record
'account_id', //primary key column name for this model's table
'id' //primary key column name for related model's table
);
}
}
And
class Location extends Model
{
public function account_doctors()
{
return $this->morphedByMany(
AccountDoctor::class,
'locationable', //name for the morphable
'locationables', //pivot table
'location_id', //foreign key on the pivot table to identify this model record
'locationable_id', //foreign key on the pivot table to identify related model record
'id', //primary key column name for this model's table
'account_id' //primary key column name for related model's
);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.