0

I have there tables:

// Itinerary
id

// Location
id

// Popup
id

// Poi
id
itinerary_id
poiable_id
poiable_type

// Hotspot
id
content_id
content_type

A Itinerary hasMany Locations and Popups through POI (poiable, polymorphic)

class Itinerary extends Model {
...
  public function pois()
  {
    return $this->hasMany('App\Models\Poi');
  }

  public function popups()
  {
    return $this->morphedByMany('App\Models\Popup', 'poiable', 'poi');
  }

  public function locations()
  {
    return $this->morphedByMany('App\Models\Location', 'poiable', 'poi');
  }
}

Locations and Popups hasMany Hotspots (polymorphic via content_id, content_type). A Hotspot holds coords. A location or popup can be opened from multiple angles in an itinerary but holds the same information.

itinerary -> poi -> location or popup -> hotspot

What I'm trying to do is to return all the hotspots of an itinerary, it must be a Relation to keep existing code intact.

Something in the line of:

function hotspots() {
return $this->hasManyThrough('App\Models\Hotspot', 'App\Models\Poi', 'itinerary_id', 'content_id', 'id', 'poiable_id')->join('poi', 'hotspot.content_type', '=', 'poi.poiable_type')
}

but can't get it to work (Syntax error or access violation: 1066 Not unique table/alias:)

Jareish
  • 772
  • 2
  • 9
  • 24

1 Answers1

0

You don't need to join the poi table, that's already done by the relationship (hence the error):

public function hotspots()
{
    return $this->hasManyThrough(
        Hotspot::class, Poi::class, 'itinerary_id', 'content_id', 'id', 'poiable_id'
    )->whereColumn('hotspot.content_type', '=', 'poi.poiable_type');
}
Jonas Staudenmeir
  • 24,815
  • 6
  • 63
  • 109