Skip to content

Commit

Permalink
Simplifies collection customisation (#46217)
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald authored Feb 22, 2023
1 parent c99d45b commit ad021d8
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Illuminate/Http/Resources/Json/JsonResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,24 @@ public static function make(...$parameters)
*/
public static function collection($resource)
{
return tap(new AnonymousResourceCollection($resource, static::class), function ($collection) {
return tap(static::newCollection($resource), function ($collection) {
if (property_exists(static::class, 'preserveKeys')) {
$collection->preserveKeys = (new static([]))->preserveKeys === true;
}
});
}

/**
* Create a new resource collection instance.
*
* @param mixed $resource
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
*/
protected static function newCollection($resource)
{
return new AnonymousResourceCollection($resource, static::class);
}

/**
* Resolve the resource to an array.
*
Expand Down

0 comments on commit ad021d8

Please sign in to comment.