-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.5] Add a "qualifyColumn" method to the Eloquent Model #22577
Conversation
*/ | ||
public function qualify($column) | ||
{ | ||
if (Str::contains($column, '.')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this? What if my column name actually has dots in it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have an example for a column with dots?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What example do you need? Any name with dot. user.name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can add a comment here, something like:
Don't qualify the column if a
.
is found, in which case it's assume to be qualified already
I just hope (not an SQL expert here) it's not possible to have a actual column name with a dot ... in which case the worst output would be that the qualification wouldn't be performed and the user would have to do it manually 🤷♀️
return $column; | ||
} | ||
|
||
return $this->getTable().'.'.$column; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this is missing quotations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh, what?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean quotations of the table name and the column name. We already have this available via Grammar::wrap().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would this suddenly be an issue? I mean I understand your idea, but this PR is only refactoring existing code doing exactly what this line does, nothing else.
I think if grammar shall be involved in this, it should be a separate PR anyway.
We might want to rename this to something like |
@taylorotwell I changed it to |
This is cool, nice addition. Sort of wish it was just Group::whereHas('users', function ($query) {
$query->whereNull(GroupUser::column('deleted_at'));
}); For now I've got around this by just adding this static method to my base class: abstract class Model extends Eloquent
{
public static function column($column)
{
return (new static())->qualifyColumn($column);
}
} |
Useful in complex queries where you need to qualify the column name to avoid collision: