Support the ongoing development of Laravel.io →
Database Eloquent

Basically, I'm trying to use the following table structure in Eloquent. I have a polymorphic table that connects people to organizations. There are some sub organization tables and I gave the district one as an example that I want to connect Person to.

The thing to see here is that the OrganizationPerson.organization_id is pointing to the District.organization_id instead of District.id

Person
  id

OrganizationPerson
  id
  person_id
  organization_id
  organization_type

District
  id
  organization_id

Organization
  id
  name

The table data would look like

Person
  id => 1

OrganizationPerson
  id => 4
  person_id => 1
  organization_id => 2
  organization_type => 'District'

District
  id => 3
  organization_id => 2

Organization
  id => 2
  name => 'North Central District'

How would I get this to work?

class Person extends Model {
  public function district()
  {
    return $this->morphTo(); // Something or whatever, how does it work?
  }
}

Do I need to create a custom morphToOne or hasOneThrough

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

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.

© 2025 Laravel.io - All rights reserved.