Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Fix attribute mutator access in loadMissing #53879

Conversation

SychO9
Copy link
Contributor

@SychO9 SychO9 commented Dec 13, 2024

Issue
When using ->loadMissing collection method on a nested relationship it can cause all the models in the collection to be converted to JSON which causes attribute mutators to be accessed. This is normally not a problem, unless the mutators need to access other relationships, in which case it can become an issue as certain relationships can get loaded unnecessarily.

Proposed Solution
The cause is the use of ->whereNotNull collection method, which leads to the JSON conversion. The PR changes that bit to a simpler ->filter access.

Additional Context
Backtrace when using ->whereNotNull

array:105 [▼
  ...app code  

  8 => array:3 [▼
    "file" => "/home/.../vendor/illuminate/database/Eloquent/Concerns/HasAttributes.php"
    "line" => 689
    "function" => "getContentAttribute"
  ]
  9 => array:3 [▼
    "file" => "/home/.../vendor/illuminate/database/Eloquent/Concerns/HasAttributes.php"
    "line" => 739
    "function" => "mutateAttribute"
  ]
  10 => array:3 [▼
    "file" => "/home/.../vendor/illuminate/database/Eloquent/Concerns/HasAttributes.php"
    "line" => 277
    "function" => "mutateAttributeForArray"
  ]
  11 => array:3 [▼
    "file" => "/home/.../vendor/illuminate/database/Eloquent/Concerns/HasAttributes.php"
    "line" => 215
    "function" => "addMutatedAttributesToArray"
  ]
  12 => array:3 [▼
    "file" => "/home/.../vendor/illuminate/database/Eloquent/Model.php"
    "line" => 1664
    "function" => "attributesToArray"
  ]
  13 => array:3 [▼
    "file" => null
    "line" => null
    "function" => "Illuminate\Database\Eloquent\{closure}"
  ]
  14 => array:3 [▼
    "file" => "/home/.../vendor/illuminate/database/Eloquent/Concerns/PreventsCircularRecursion.php"
    "line" => 46
    "function" => "call_user_func"
  ]
  15 => array:3 [▼
    "file" => "/home/.../vendor/illuminate/database/Eloquent/Model.php"
    "line" => 1663
    "function" => "withoutRecursion"
  ]
  16 => array:3 [▼
    "file" => "/home/.../vendor/illuminate/database/Eloquent/Model.php"
    "line" => 1695
    "function" => "toArray"
  ]
  17 => array:3 [▼
    "file" => "/home/.../vendor/illuminate/database/Eloquent/Model.php"
    "line" => 1680
    "function" => "jsonSerialize"
  ]
  18 => array:3 [▼
    "file" => "/home/.../vendor/illuminate/database/Eloquent/Model.php"
    "line" => 2391
    "function" => "toJson"
  ]
  19 => array:3 [▼
    "file" => "/home/.../vendor/illuminate/support/helpers.php"
    "line" => 68
    "function" => "__toString"
  ]
  20 => array:3 [▼
    "file" => "/home/.../vendor/illuminate/support/helpers.php"
    "line" => 166
    "function" => "blank"
  ]
  21 => array:3 [▼
    "file" => "/home/.../vendor/illuminate/support/helpers.php"
    "line" => 483
    "function" => "filled"
  ]
  22 => array:3 [▼
    "file" => "/home/.../vendor/illuminate/collections/functions.php"
    "line" => 20
    "function" => "transform"
  ]
  23 => array:3 [▼
    "file" => "/home/.../vendor/illuminate/collections/Traits/EnumeratesValues.php"
    "line" => 1097
    "function" => "Illuminate\Support\enum_value"
  ]
  24 => array:3 [▼
    "file" => null
    "line" => null
    "function" => "Illuminate\Support\Traits\{closure}"
  ]
  25 => array:3 [▼
    "file" => "/home/.../vendor/illuminate/collections/Arr.php"
    "line" => 930
    "function" => "array_filter"
  ]
  26 => array:3 [▼
    "file" => "/home/.../vendor/illuminate/collections/Collection.php"
    "line" => 395
    "function" => "where"
  ]
  27 => array:3 [▼
    "file" => "/home/.../vendor/illuminate/collections/Traits/EnumeratesValues.php"
    "line" => 623
    "function" => "filter"
  ]
  28 => array:3 [▼
    "file" => "/home/.../vendor/illuminate/collections/Traits/EnumeratesValues.php"
    "line" => 645
    "function" => "where"
  ]
  29 => array:3 [▼
    "file" => "/home/.../vendor/illuminate/database/Eloquent/Collection.php"
    "line" => 274
    "function" => "whereNotNull"
  ]
  30 => array:3 [▼
    "file" => "/home/.../vendor/illuminate/database/Eloquent/Collection.php"
    "line" => 245
    "function" => "loadMissingRelation"
  ]
  31 => array:3 [▼
    "file" => "/home/.../vendor/flarum/core/src/Api/Endpoint/Concerns/HasEagerLoading.php"
    "line" => 128
    "function" => "loadMissing"
  ],
  
  ...app code
];

@taylorotwell taylorotwell merged commit 275539a into laravel:11.x Dec 13, 2024
40 checks passed
@SychO9 SychO9 deleted the sm/load-missing-unnecessary-attribute-mutator-access branch December 13, 2024 16:06
@mortenscheel
Copy link

@SychO9 thanks! This issue very nearly drove me insane.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants