Hi,
I had a similar issue and the only I found to accomplish this pattern using polymorphic relations is to extend the pivot table. In your example I would do this:
Locatable:
id
location_id -> references id on Location
locatable_type
locatable_id
endinglocatable_id (or whatever name you want in order to model the fact that a single model can have more than one location associated with)
Then in the Journey model class I would implement two distinct relation methods:
public function startingLocations()
{
return $this->morphToMany('Location', 'locatable');
}
public function endingLocations()
{
return $this->morphToMany('Location', 'locatable',null,'endinglocatable_id');
}
It has worked for me. I don't if it is either the "correct" or the most elegant way to solve this, but I hope this helps.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community