-
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
Query Builder fails on a ManyToMany relationship that has a model with an ID that is cast to an Enum #49735
Comments
Hey there, thanks for reporting this issue. We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.
Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue. Thanks! |
Sorry for the delay. I'm unfortunately unable to use In the meantime, I did my best to revamp the minimum reproducible example I had already provided so that things are better separated. I have also updated the description of this issue so that it is more in line with the MRE repo. After checking out the proper commits (see below), all that is needed is a
In both cases, for a quick start, please look at The first issueIn the MRE repo are three models: With this setup, running the following query The second issueIn the MRE repo, the model With this setup, running the following query It's notable that removing the cast for |
Thanks for your report! We'd love to see a PR for this. |
Thank you for reporting this issue! As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub. If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team. Thank you! |
Hello, I submitted PR #49787 . The PR description describes what it fixes. This PR should fix the two issues at hand; BUT, I have suspicions that there are more places where BackedEnums are not properly handled. I have tried to find some more in the couple of hours I had to familiarize myself with the code. One that stood out to me is In general, my familiarity with the code is pretty limited. I would very much like to contribute some more, so any pointers on the following are very much appreciated :
Thank you. |
@sidquisaad I'd approach this with waiting until someone asks for support for other portions of the framework. |
… BackedEnums (#49787) - Illuminate\Database\Eloquent\Relations\BelongsTo::getForeignKeyFrom() handles BackedEnums properly See #49735 Co-authored-by: Saad <[email protected]>
We merged the PR for this one. Will be tagged today. |
Laravel Version
10.10
PHP Version
8.2.14
Database Driver & Version
PostgreSQL 15.1 (Ubuntu 15.1-1.pgdg20.04+1)
Description
A
ManyToMany
relationship in which one or more parties casts the id column to anEnum
will causeIlluminate\Database\Query\Builder::whereIntegerInRaw()
to throw anErrorException
because it tries to cast the (already cast to an enum) id on the model to anint
. However,Enum
s are NOT castable in PHP (I tried php 8.1 up to 8.3).Steps To Reproduce
A minimal reproducible example is available here.
In the MRE repo are three models:
User
,Role
, and a pivotRoleUser
.A regular
ManyToMany
relationship is defined betweenUser
andRole
through theRoleUser
pivot.Role
'sid
column is an integer column that is cast to the backed enumApp\Enum\Roles
.Similarly, inside the pivot
RoleUser
,role_id
is also cast to the same enum.With this setup, running the following query
Role::with('users')->get();
throws anErrorException
with the message "Object of class App\Enums\Roles could not be converted to int", inIlluminate\Database\Query\Builder::whereIntegerInRaw()
, line 1164.EDIT:
The fact that the concerned column is autoincrement or not has no incidence on the issue. I have updated various sections of this issue to reflect this.
The text was updated successfully, but these errors were encountered: