Skip to content

[12.x] Refactor: remove Arr::wrap() and add Collection::wrap()#58748

Merged
taylorotwell merged 1 commit intolaravel:12.xfrom
alipowerful7:refactor/use-collection-wrap
Feb 10, 2026
Merged

[12.x] Refactor: remove Arr::wrap() and add Collection::wrap()#58748
taylorotwell merged 1 commit intolaravel:12.xfrom
alipowerful7:refactor/use-collection-wrap

Conversation

@alipowerful7
Copy link
Contributor

[12.x] Use Collection::wrap in sparseIncluded() method

This PR updates the sparseIncluded() method to use Collection::wrap() instead of new Collection(), following the improvements introduced in #53891.

Changes

  • Replaced new Collection(...) with Collection::wrap() for cleaner, more consistent code
  • Maintains the same functionality while improving code clarity
  • In cases where the value is already Enumerable, this approach is more efficient

Before

        return transform($this->cachedSparseIncluded[$key] ?? null, function ($value) {
            return (new Collection(Arr::wrap($value)))
                ->transform(function ($item) {
                    $item = implode('.', Arr::take(explode('.', $item), JsonApiResource::$maxRelationshipDepth - 1));

                    return ! empty($item) ? $item : null;
                })->filter()->all();
        }) ?? [];

After

        return transform($this->cachedSparseIncluded[$key] ?? null, function ($value) {
            return Collection::wrap($value)
                ->transform(function ($item) {
                    $item = implode('.', Arr::take(explode('.', $item), JsonApiResource::$maxRelationshipDepth - 1));

                    return ! empty($item) ? $item : null;
                })->filter()->all();
        }) ?? [];

This change aligns with the framework's preference for using Collection::wrap() when creating collections, as established in #53891.

@taylorotwell taylorotwell merged commit 4345305 into laravel:12.x Feb 10, 2026
72 checks passed
@alipowerful7 alipowerful7 deleted the refactor/use-collection-wrap branch February 10, 2026 22:21
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.

2 participants

Comments