Support the ongoing development of Laravel.io →
Database Eloquent Architecture
Last updated 2 years ago.
0

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.

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

nickb777 nickb777 Joined 8 Mar 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.