Skip to content

Eloquent whereAttachedTo incorrectly querying non-ID columns from the model #55665

Closed
@ashleyshenton

Description

@ashleyshenton

Laravel Version

12.10.1

PHP Version

8.4.6

Database Driver & Version

MySQL 8.0.33 for macOS 15.4.1 (DBngin)

Description

When using the whereAttachedTo method, the entire model and all its properties are sent into whereKey which then converts all columns to an integer and adds them to the whereIn clause. This means a bunch of 0's are added for empty strings / null values, which is unlikely to cause an issue but is still unintended behaviour. But integer and datetime columns add a bunch of potentially valid IDs which should not be included in the query.

Image

Steps To Reproduce

Create a belongsToMany or MorphToMany relationship between two models.

Write an Eloquent query including the whereAttachedTo method and pass the other model in. Either call ddRawSql on this query or call get and inspect the DB query log.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions