Skip to content

Query Builder fails on a ManyToMany relationship that has a model with an ID that is cast to an Enum #49735

Closed
@saadsidqui

Description

@saadsidqui

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, Enums 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions