class Collection extends Collection
Methods
This is done to prevent conflicts when using visibility scopes.
The original Laravel logic uses ->whereNotNull() which is an abstraction that unnecessarily causes attribute mutators to run, so if a mutator relies on an eager loaded relationship, the mutator will be executed before the call to ->loadMissing() is over.
Details
at
line 47
Collection
loadAggregate($relations, $column, $function = null)
This is done to prevent conflicts when using visibility scopes.
Without this, we get the following example query when using a visibility scope and eager loading the count of `mentionedBy`:. ```sql SELECT `id`, ( SELECT count(*) FROM `posts` AS `laravel_reserved_0` INNER JOIN `post_mentions_post` ON `laravel_reserved_0`.`id` = `post_mentions_post`.`post_id` WHERE `posts`.`id` = `post_mentions_post`.`mentions_post_id` --- ^^^^^^^ this is the problem, visibility scopes always assume the default table name, rather than --- the Laravel auto-generated alias. AND `TYPE` in ('discussionTagged', 'discussionStickied', 'discussionLocked', 'comment', 'discussionRenamed') ) AS `mentioned_by_count` FROM `posts` WHERE `posts`.`id` in (23642) ``` So by applying an alias on the parent query, we prevent Laravel from auto aliasing the sub-query.
at
line 65
protected void
loadMissingRelation(Collection $models, array $path)
The original Laravel logic uses ->whereNotNull() which is an abstraction that unnecessarily causes attribute mutators to run, so if a mutator relies on an eager loaded relationship, the mutator will be executed before the call to ->loadMissing() is over.
We replace it with a simple ->where(fn (mixed $relation) => $relation !== null) to avoid this issue.