Description
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 an Enum
will cause Illuminate\Database\Query\Builder::whereIntegerInRaw()
to throw an ErrorException
because it tries to cast the (already cast to an enum) id on the model to an int
. 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 pivot RoleUser
.
A regular ManyToMany
relationship is defined between User
and Role
through the RoleUser
pivot.
Role
's id
column is an integer column that is cast to the backed enum App\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 an ErrorException
with the message "Object of class App\Enums\Roles could not be converted to int", in Illuminate\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.