Support the ongoing development of Laravel.io →
posted 8 years ago
Eloquent

I have a method Assets.

It has this this relation:

	public function fields() {
	    return $this->hasManyThrough( CustomField::class, CustomEntity::class,
            'id', 'entity_id', 'entity_id' );
    }

If I do $asset->fields, the problem is that $asset->fields->id, the id is coming from CustomEntity rather than from CustomField. This is due to the query that is created:

select `custom_fields`.*, `custom_entities`.`id`
 from `custom_fields`
 inner join `custom_entities`
  on `custom_entities`.`id` = `custom_fields`.`entity_id`
 where `custom_entities`.`id` = 14

The query would work without the custom_entites.id:

select `custom_fields`.*
 from `custom_fields`
 inner join `custom_entities`
  on `custom_entities`.`id` = `custom_fields`.`entity_id`
 where `custom_entities`.`id` = 14

Am I doing something wrong or is this a bug?

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.