Skip to content

Use whereKey query method where possible#401

Open
imliam wants to merge 2 commits intodriftingly:mainfrom
imliam:where-key
Open

Use whereKey query method where possible#401
imliam wants to merge 2 commits intodriftingly:mainfrom
imliam:where-key

Conversation

@imliam
Copy link
Copy Markdown
Contributor

@imliam imliam commented Oct 4, 2025

- User::where('id', '=', $user->id)->get();
+ User::whereKey($user)->get();

Comment on lines +84 to +90
if ($argCount === 2) {
return $this->refactorTwoArgumentWhere($node, $args, $isWhereNot);
}

if ($argCount === 3 && $isWhere) {
return $this->refactorThreeArgumentWhere($node, $args);
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

can these be condensed into a single method? they look quite similar

}

$propertyFetch = $args[1]->value;
if (! $this->isName($propertyFetch->name, 'id')) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

instead of hardcoding id here, it would be better to get a new model instance from the helpers, and then use the result of ->getKeyName()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That's an essential point. Not every system has the 'id' as the primary key.

@calebdw
Copy link
Copy Markdown
Collaborator

calebdw commented Dec 23, 2025

I'm not sure if there's another rule that does this, but I'd also like a rule (or just update this rule) that turns whereIn('id', ...) into whereKey() as well

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.

3 participants