Skip to content

MorphTo relations are broken in some cases since v11.33.0 #53624

@sebj54

Description

@sebj54

Laravel Version

11.33.0

PHP Version

8.3.13

Database Driver & Version

No response

Description

Since version 11.33.0 and more precisely this commit: 8528d04, the constraints applied on MorphTo relations have changed.

For the example, let's say I have the example relation from the docs: https://laravel.com/docs/11.x/eloquent-relationships#one-to-one-polymorphic-model-structure. So I have an Image model with an imageable relation (MorphTo), pointing to a Post.

Before that commit, the foreign key constraint was correctly added to the relation query: it added a where like this:

[
        "type" => "Basic"
        "column" => "posts.id"
        "operator" => "="
        "value" => 252
        "boolean" => "and"
]

Now with the latest version, the only where is:

[
        "type" => "Null"
        "column" => ""
        "boolean" => "and"
]

This bug breaks some packages like https://github.com/kirschbaum-development/eloquent-power-joins.

Steps To Reproduce

  • Create a MorphTo relation (for example Image::imageable)
  • Call the relation: $relation = (new Image)->imageable()
  • Dump the relation and watch the wheres of the query
  • Change your Laravel version from 11.32.x to 11.33.x

Feel free to ask if you need a proper reproduction to work on this but I think you have some test projects already covering this feature.

Thanks for your help!

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