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

[10.x] Update DatabaseRule to handle Enums for simple where clause #47679

Merged
merged 1 commit into from Jul 8, 2023

Conversation

calebdw
Copy link
Contributor

@calebdw calebdw commented Jul 7, 2023

Hello!

This PR updates the DatabaseRule trait (used by Rule::unique and others) to automatically cast Enums to their value like the Query class does. For example, this throws an exception:

Rule::unique(<class>)->where('status', Enum::Case);

whereas this does work in a normal query:

Model::query()->where('status', Enum::Case);

Thanks!

@taylorotwell
Copy link
Member

Does it work for whereIn?

@taylorotwell taylorotwell marked this pull request as draft July 8, 2023 20:45
@taylorotwell taylorotwell marked this pull request as ready for review July 8, 2023 20:46
@calebdw
Copy link
Contributor Author

calebdw commented Jul 8, 2023

@taylorotwell,

whereIn calls are being forwarded to the underlying query using a closure (so therefore it works by default):

public function whereIn($column, $values)
{
return $this->where(function ($query) use ($column, $values) {
$query->whereIn($column, $values);
});
}

The reason why it was throwing an exception for where is because the simple wheres are being converted to their string equivalents instead of also being forwarded:

$this->wheres[] = compact('column', 'value');

protected function formatWheres()
{
return collect($this->wheres)->map(function ($where) {
return $where['column'].','.'"'.str_replace('"', '""', $where['value']).'"';
})->implode(',');
}

I just made the easy change by converting the enum to it's backed value before it's ultimately converted to a string...it might be a better idea to remove the where string conversion/reparsing and just send everything to the underlying query.

@taylorotwell taylorotwell merged commit 6a2dff3 into laravel:10.x Jul 8, 2023
17 checks passed
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.

None yet

2 participants