Skip to content

[2.x] Nested eager loading causes an undefined relationship error #5042

Description

@rafaucau

Current Behavior

Initially reported here: https://discuss.flarum.org/d/5791-masquerade-by-friendsofflarum-the-user-profile-builder/230. I found that the issue is in Flarum core.

Using eagerLoadWhere() with a nested relationship can cause an API request to fail with:

Call to undefined relationship [user] on model [Flarum\User\User].

Moving the nested load into a with() callback inside eagerLoadWhere('user', ...) resolves the error.

Steps to Reproduce

See this code: https://github.com/FriendsOfFlarum/masquerade/blob/40c4f6284c56810c6231b189e1e4c6bddd138d60/extend.php#L105-L107
Workaround: https://github.com/FriendsOfFlarum/masquerade/pull/120/changes

Expected Behavior

Loading a nested relationship should not cause an undefined relationship error.

Screenshots

No response

Environment

  • Flarum version: v2.0.0-rc.8

Output of php flarum info

No response

Possible Solution

No response

Additional Context

AI-assisted analysis (GPT Astra-6):

The suspected cause is in getWhereEagerLoadsFor() in:

$subRelations[$relation] = Str::after($relation, "$included.");

This keeps the full relationship path as the key and replaces the constraint callback with a string. EloquentBuffer passes the result to with() on the related model's query.

The suggested fix is to use the relative path as the key and preserve the callback, passing it the query and context. This core change has not been tested.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions