Skip to content

Commit 37303ad

Browse files
[8.x] Change User to UserResource (#6714)
* change User to UserResource * change all available instances of User resource to UserResource * Update eloquent-resources.md Co-authored-by: Taylor Otwell <[email protected]>
1 parent 1e2b5c8 commit 37303ad

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

eloquent-resources.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Of course, you may always convert Eloquent models or collections to JSON using t
2424

2525
To generate a resource class, you may use the `make:resource` Artisan command. By default, resources will be placed in the `app/Http/Resources` directory of your application. Resources extend the `Illuminate\Http\Resources\Json\JsonResource` class:
2626

27-
php artisan make:resource User
27+
php artisan make:resource UserResource
2828

2929
<a name="generating-resource-collections"></a>
3030
#### Resource Collections
@@ -42,15 +42,15 @@ To create a resource collection, you should use the `--collection` flag when cre
4242

4343
> {tip} This is a high-level overview of resources and resource collections. You are highly encouraged to read the other sections of this documentation to gain a deeper understanding of the customization and power offered to you by resources.
4444
45-
Before diving into all of the options available to you when writing resources, let's first take a high-level look at how resources are used within Laravel. A resource class represents a single model that needs to be transformed into a JSON structure. For example, here is a simple `User` resource class:
45+
Before diving into all of the options available to you when writing resources, let's first take a high-level look at how resources are used within Laravel. A resource class represents a single model that needs to be transformed into a JSON structure. For example, here is a simple `UserResource` resource class:
4646

4747
<?php
4848

4949
namespace App\Http\Resources;
5050

5151
use Illuminate\Http\Resources\Json\JsonResource;
5252

53-
class User extends JsonResource
53+
class UserResource extends JsonResource
5454
{
5555
/**
5656
* Transform the resource into an array.
@@ -74,7 +74,7 @@ Every resource class defines a `toArray` method which returns the array of attri
7474

7575
Note that we can access model properties directly from the `$this` variable. This is because a resource class will automatically proxy property and method access down to the underlying model for convenient access. Once the resource is defined, it may be returned from a route or controller. The resource accepts the underlying model instance via its constructor:
7676

77-
use App\Http\Resources\User as UserResource;
77+
use App\Http\Resources\UserResource;
7878
use App\Models\User;
7979

8080
Route::get('/user/{id}', function ($id) {
@@ -86,7 +86,7 @@ Note that we can access model properties directly from the `$this` variable. Thi
8686

8787
If you are returning a collection of resources or a paginated response, you should use the `collection` method provided by your resource class when creating the resource instance in your route or controller:
8888

89-
use App\Http\Resources\User as UserResource;
89+
use App\Http\Resources\UserResource;
9090
use App\Models\User;
9191

9292
Route::get('/users', function () {
@@ -144,7 +144,7 @@ When returning a resource collection from a route, Laravel resets the collection
144144

145145
use Illuminate\Http\Resources\Json\JsonResource;
146146

147-
class User extends JsonResource
147+
class UserResource extends JsonResource
148148
{
149149
/**
150150
* Indicates if the resource's collection keys should be preserved.
@@ -156,7 +156,7 @@ When returning a resource collection from a route, Laravel resets the collection
156156

157157
When the `preserveKeys` property is set to `true`, collection keys will be preserved when the collection is returned from a route or controller:
158158

159-
use App\Http\Resources\User as UserResource;
159+
use App\Http\Resources\UserResource;
160160
use App\Models\User;
161161

162162
Route::get('/users', function () {
@@ -166,9 +166,9 @@ When the `preserveKeys` property is set to `true`, collection keys will be prese
166166
<a name="customizing-the-underlying-resource-class"></a>
167167
#### Customizing The Underlying Resource Class
168168

169-
Typically, the `$this->collection` property of a resource collection is automatically populated with the result of mapping each item of the collection to its singular resource class. The singular resource class is assumed to be the collection's class name without the trailing `Collection` portion of the class name.
169+
Typically, the `$this->collection` property of a resource collection is automatically populated with the result of mapping each item of the collection to its singular resource class. The singular resource class is assumed to be the collection's class name without the trailing `Collection` portion of the class name. In addition, depending on your personal preference, the singular resource class may or may not be suffixed with `Resource`.
170170

171-
For example, `UserCollection` will attempt to map the given user instances into the `User` resource. To customize this behavior, you may override the `$collects` property of your resource collection:
171+
For example, `UserCollection` will attempt to map the given user instances into the `UserResource` resource. To customize this behavior, you may override the `$collects` property of your resource collection:
172172

173173
<?php
174174

@@ -199,7 +199,7 @@ In essence, resources are simple. They only need to transform a given model into
199199

200200
use Illuminate\Http\Resources\Json\JsonResource;
201201

202-
class User extends JsonResource
202+
class UserResource extends JsonResource
203203
{
204204
/**
205205
* Transform the resource into an array.
@@ -221,7 +221,7 @@ In essence, resources are simple. They only need to transform a given model into
221221

222222
Once a resource has been defined, it may be returned directly from a route or controller:
223223

224-
use App\Http\Resources\User as UserResource;
224+
use App\Http\Resources\UserResource;
225225
use App\Models\User;
226226

227227
Route::get('/user/{id}', function ($id) {
@@ -231,9 +231,9 @@ Once a resource has been defined, it may be returned directly from a route or co
231231
<a name="relationships"></a>
232232
#### Relationships
233233

234-
If you would like to include related resources in your response, you may add them to the array returned by your resource's `toArray` method. In this example, we will use the `Post` resource's `collection` method to add the user's blog posts to the resource response:
234+
If you would like to include related resources in your response, you may add them to the array returned by your resource's `toArray` method. In this example, we will use the `PostResource` resource's `collection` method to add the user's blog posts to the resource response:
235235

236-
use App\Http\Resources\Post;
236+
use App\Http\Resources\PostResource;
237237

238238
/**
239239
* Transform the resource into an array.
@@ -247,7 +247,7 @@ If you would like to include related resources in your response, you may add the
247247
'id' => $this->id,
248248
'name' => $this->name,
249249
'email' => $this->email,
250-
'posts' => Post::collection($this->posts),
250+
'posts' => PostResource::collection($this->posts),
251251
'created_at' => $this->created_at,
252252
'updated_at' => $this->updated_at,
253253
];
@@ -260,7 +260,7 @@ If you would like to include related resources in your response, you may add the
260260

261261
While resources transform a single model into an array, resource collections transform a collection of models into an array. However, it is not absolutely necessary to define a resource collection class for each one of your models since all resources provide a `collection` method to generate an "ad-hoc" resource collection on the fly:
262262

263-
use App\Http\Resources\User as UserResource;
263+
use App\Http\Resources\UserResource;
264264
use App\Models\User;
265265

266266
Route::get('/users', function () {
@@ -331,7 +331,7 @@ If you would like to use a custom key instead of `data`, you may define a `$wrap
331331

332332
use Illuminate\Http\Resources\Json\JsonResource;
333333

334-
class User extends JsonResource
334+
class UserResource extends JsonResource
335335
{
336336
/**
337337
* The "data" wrapper that should be applied.
@@ -551,7 +551,7 @@ In addition to conditionally loading attributes, you may conditionally include r
551551

552552
The `whenLoaded` method may be used to conditionally load a relationship. In order to avoid unnecessarily loading relationships, this method accepts the name of the relationship instead of the relationship itself:
553553

554-
use App\Http\Resources\Post;
554+
use App\Http\Resources\PostResource;
555555

556556
/**
557557
* Transform the resource into an array.
@@ -698,7 +698,7 @@ You may also add top-level data when constructing resource instances in your rou
698698

699699
As you have already read, resources may be returned directly from routes and controllers:
700700

701-
use App\Http\Resources\User as UserResource;
701+
use App\Http\Resources\UserResource;
702702
use App\Models\User;
703703

704704
Route::get('/user/{id}', function ($id) {
@@ -707,7 +707,7 @@ As you have already read, resources may be returned directly from routes and con
707707

708708
However, sometimes you may need to customize the outgoing HTTP response before it is sent to the client. There are two ways to accomplish this. First, you may chain the `response` method onto the resource. This method will return an `Illuminate\Http\JsonResponse` instance, giving you full control over the response's headers:
709709

710-
use App\Http\Resources\User as UserResource;
710+
use App\Http\Resources\UserResource;
711711
use App\Models\User;
712712

713713
Route::get('/user', function () {
@@ -724,7 +724,7 @@ Alternatively, you may define a `withResponse` method within the resource itself
724724

725725
use Illuminate\Http\Resources\Json\JsonResource;
726726

727-
class User extends JsonResource
727+
class UserResource extends JsonResource
728728
{
729729
/**
730730
* Transform the resource into an array.

0 commit comments

Comments
 (0)