Proposal: Add Support for Aliases in with() for Eager Loading #56107
Replies: 5 comments 4 replies
-
I like the concept but I don't like the proposed syntax. I would propose as an alternative:
|
Beta Was this translation helpful? Give feedback.
-
We solved this by declaring the alias as another relation name: public function relation(): HasOne
{
...
}
public function relationAlias(): HasOne
{
return $this->relation()->where(...); // or ->scope().
} Regarding the clashes between relation names and attributes and model properties we also introduced ->a for attributes and ->r for relations. Your proposal will generate bugs because the alias will not be autocompleted nor will it be expected for other devs that did not introduce the alias because the function is not defined in the model. |
Beta Was this translation helpful? Give feedback.
-
While this reads nicely, I'd prefer not having to have to split a string, meaning, there should be literals for the relation and the alias rather than only dynamically calculated ones at runtime. However, I'm not sure how this can be done best New
|
✅ literals exist for both relation and alias | ❌ alias has to be defined on the right-hand side rather than on the same side as the relation which can be counterintuitive |
New #[Alias]
attribute
User::with([
'skills' => #[Alias('satisfied_skills')] fn ($query) => $query->whereIn('id', $requiredSkillIds)
])->get();
✅ literals exist for both relation and alias | ❌ reflection comes with a runtime cost |
❌ reflection is probably not Taylor's most preferred solution |
Beta Was this translation helpful? Give feedback.
-
Instead of changing how The only downside is that you wouldd have to call it multiple times if you want to load several aliases. User::query()
->withAliased('skills', 'missing_skills', fn ($query) =>
$query->whereNotIn('id', [1, 2, 3])
)
->withAliased('skills', 'recommended_skills', fn ($query) =>
$query->where('recommended', true)
)
->get(); |
Beta Was this translation helpful? Give feedback.
-
A potential problem with this is that it leads to dynamic relationships which become hard to document. Right now I define all of my relationships as properties to make things like phpstan happy plus provide context to IDEs. With this, For that reason I actually prefer the solution mentioned in #56107 (comment) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, Laravel's
with()
method for eager loading always attaches the loaded relation using the original relation name. This becomes restrictive when:There’s no built-in way to assign a custom name to an eager-loaded relation.
Proposed Feature
Allow an alias to be provided directly in the with() array using the syntax:
This would attach the results of the eager load to the model as:
instead of:
Benefits
Notes
The alias would only affect how the relation is attached to the model.
Internally, Laravel could split the key at " as " and treat the left side as the relation name, the right side as the alias.
This pattern is already familiar from SQL and can be implemented without breaking existing behavior.
Backward Compatibility
This would be fully backward-compatible, since:
Summary
Adding support for relation as alias syntax in Laravel's
with()
method would make eager loading more flexible, more expressive, and better suited for modern applications, especially when building APIs or reusing filtered relationships.Beta Was this translation helpful? Give feedback.
All reactions