8000 [12.x] Add resource helper functions to Model/Collections by TimKunze96 · Pull Request #55107 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[12.x] Add resource helper functions to Model/Collections #55107

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

Merged
merged 18 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add resource helper functions
  • Loading branch information
TimKunze96 committed Mar 20, 2025
commit 0909ce1da6ff1f57d1ab54ba40daab449bfed35e
14 changes: 14 additions & 0 deletions src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Illuminate\Contracts\Queue\QueueableEntity;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection as BaseCollection;
use LogicException;
Expand Down Expand Up @@ -843,4 +845,16 @@ public function toQuery()

return $model->newModelQuery()->whereKey($this->modelKeys());
}


/**
* Create a new resource collection instance for the given resource.
*
* @param class-string<JsonResource> $resourceClass
* @return ResourceCollection
*/
public function toResourceCollection(string $resourceClass): ResourceCollection
{
return $resourceClass::collection($this);
}
}
12 changes: 12 additions & 0 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Illuminate\Database\Eloquent\Relations\Concerns\AsPivot;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection as BaseCollection;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -2459,4 +2460,15 @@ public function __wakeup()

$this->initializeTraits();
}

/**
* Create a new resource object for the given resource.
*
* @param class-string<JsonResource> $resourceClass
* @return JsonResource
*/
public function toResource(string $resourceClass): JsonResource
{
return $resourceClass::make($this);
}
}
13 changes: 13 additions & 0 deletions src/Illuminate/Pagination/AbstractPaginator.php
913E
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Closure;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\ForwardsCalls;
Expand Down Expand Up @@ -799,4 +801,15 @@ public function __toString()
{
return (string) $this->render();
}

/**
* Create a paginated resource collection.
*
* @param class-string<JsonResource> $resourceClass
* @return ResourceCollection
*/
public function toResourceCollection(string $resourceClass): ResourceCollection
{
return $resourceClass::collection($this);
}
}
Loading
0