Skip to content
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

Merged
merged 2 commits into from
Jan 1, 2018

Conversation

JosephSilber
Copy link
Member

@JosephSilber JosephSilber commented Dec 31, 2017

Useful in complex queries where you need to qualify the column name to avoid collision:

function addSomeComplexSubquery($query, $model)
{
    $query->where($model->qualifyColumn('column'), $someValue);
}

@JosephSilber JosephSilber changed the title Add a "qualify" method to the Eloquent Model [5.5] Add a "qualify" method to the Eloquent Model Dec 31, 2017
*/
public function qualify($column)
{
if (Str::contains($column, '.')) {
Copy link
Contributor

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?

Copy link
Contributor

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?

Copy link
Contributor

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

Copy link
Contributor

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;
Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh, what?

Copy link
Contributor

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().

Copy link
Contributor

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.

@taylorotwell
Copy link
Member

taylorotwell commented Dec 31, 2017

We might want to rename this to something like qualifyColumn... I fear something as generic as qualify could have conflicts with user defined methods if we release it on 5.5. I like the idea though. Cleans up some repetitive code.

@JosephSilber JosephSilber changed the title [5.5] Add a "qualify" method to the Eloquent Model [5.5] Add a "qualifyColumn" method to the Eloquent Model Dec 31, 2017
@JosephSilber
Copy link
Member Author

@taylorotwell I changed it to qualifyColumn.

@taylorotwell taylorotwell merged commit 95407bd into laravel:5.5 Jan 1, 2018
@reinink
Copy link
Contributor

reinink commented Jan 24, 2018

This is cool, nice addition. Sort of wish it was just column though, and I wish it could be called statically:

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);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants