Support the ongoing development of Laravel.io →
Database Eloquent

Hello,

I'm trying to set up a hasOneThrough relation on one table. But Eloquent will not look it the two tables are the same and add an alias for one of the tables.

The table looks something like this:

Product table:
id
parent_id
....

Company_product
id
product_id

So the end query will look like this:

select `products`.*, `products`.`parent_id` as `laravel_through_key` from `products` inner join `products` on `products`.`parent_id` = `products`.`id` where `products`.`id` in (?)

So my question is, is there an option to tell Eloquent that is should an as alias instead of the table name?

I know that I can eager loading them with chaining the relations but this would be better because I will not get any memory usage for the products that I don't even need.

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.