You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* change User to UserResource
* change all available instances of User resource to UserResource
* Update eloquent-resources.md
Co-authored-by: Taylor Otwell <[email protected]>
Copy file name to clipboardExpand all lines: eloquent-resources.md
+20-20Lines changed: 20 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ Of course, you may always convert Eloquent models or collections to JSON using t
24
24
25
25
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:
26
26
27
-
php artisan make:resource User
27
+
php artisan make:resource UserResource
28
28
29
29
<aname="generating-resource-collections"></a>
30
30
#### Resource Collections
@@ -42,15 +42,15 @@ To create a resource collection, you should use the `--collection` flag when cre
42
42
43
43
> {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.
44
44
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:
46
46
47
47
<?php
48
48
49
49
namespace App\Http\Resources;
50
50
51
51
use Illuminate\Http\Resources\Json\JsonResource;
52
52
53
-
class User extends JsonResource
53
+
class UserResource extends JsonResource
54
54
{
55
55
/**
56
56
* Transform the resource into an array.
@@ -74,7 +74,7 @@ Every resource class defines a `toArray` method which returns the array of attri
74
74
75
75
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:
76
76
77
-
use App\Http\Resources\User as UserResource;
77
+
use App\Http\Resources\UserResource;
78
78
use App\Models\User;
79
79
80
80
Route::get('/user/{id}', function ($id) {
@@ -86,7 +86,7 @@ Note that we can access model properties directly from the `$this` variable. Thi
86
86
87
87
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:
88
88
89
-
use App\Http\Resources\User as UserResource;
89
+
use App\Http\Resources\UserResource;
90
90
use App\Models\User;
91
91
92
92
Route::get('/users', function () {
@@ -144,7 +144,7 @@ When returning a resource collection from a route, Laravel resets the collection
144
144
145
145
use Illuminate\Http\Resources\Json\JsonResource;
146
146
147
-
class User extends JsonResource
147
+
class UserResource extends JsonResource
148
148
{
149
149
/**
150
150
* 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
156
156
157
157
When the `preserveKeys` property is set to `true`, collection keys will be preserved when the collection is returned from a route or controller:
158
158
159
-
use App\Http\Resources\User as UserResource;
159
+
use App\Http\Resources\UserResource;
160
160
use App\Models\User;
161
161
162
162
Route::get('/users', function () {
@@ -166,9 +166,9 @@ When the `preserveKeys` property is set to `true`, collection keys will be prese
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`.
170
170
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:
172
172
173
173
<?php
174
174
@@ -199,7 +199,7 @@ In essence, resources are simple. They only need to transform a given model into
199
199
200
200
use Illuminate\Http\Resources\Json\JsonResource;
201
201
202
-
class User extends JsonResource
202
+
class UserResource extends JsonResource
203
203
{
204
204
/**
205
205
* Transform the resource into an array.
@@ -221,7 +221,7 @@ In essence, resources are simple. They only need to transform a given model into
221
221
222
222
Once a resource has been defined, it may be returned directly from a route or controller:
223
223
224
-
use App\Http\Resources\User as UserResource;
224
+
use App\Http\Resources\UserResource;
225
225
use App\Models\User;
226
226
227
227
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
231
231
<aname="relationships"></a>
232
232
#### Relationships
233
233
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:
235
235
236
-
use App\Http\Resources\Post;
236
+
use App\Http\Resources\PostResource;
237
237
238
238
/**
239
239
* 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
@@ -260,7 +260,7 @@ If you would like to include related resources in your response, you may add the
260
260
261
261
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:
262
262
263
-
use App\Http\Resources\User as UserResource;
263
+
use App\Http\Resources\UserResource;
264
264
use App\Models\User;
265
265
266
266
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
331
331
332
332
use Illuminate\Http\Resources\Json\JsonResource;
333
333
334
-
class User extends JsonResource
334
+
class UserResource extends JsonResource
335
335
{
336
336
/**
337
337
* The "data" wrapper that should be applied.
@@ -551,7 +551,7 @@ In addition to conditionally loading attributes, you may conditionally include r
551
551
552
552
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:
553
553
554
-
use App\Http\Resources\Post;
554
+
use App\Http\Resources\PostResource;
555
555
556
556
/**
557
557
* Transform the resource into an array.
@@ -698,7 +698,7 @@ You may also add top-level data when constructing resource instances in your rou
698
698
699
699
As you have already read, resources may be returned directly from routes and controllers:
700
700
701
-
use App\Http\Resources\User as UserResource;
701
+
use App\Http\Resources\UserResource;
702
702
use App\Models\User;
703
703
704
704
Route::get('/user/{id}', function ($id) {
@@ -707,7 +707,7 @@ As you have already read, resources may be returned directly from routes and con
707
707
708
708
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:
709
709
710
-
use App\Http\Resources\User as UserResource;
710
+
use App\Http\Resources\UserResource;
711
711
use App\Models\User;
712
712
713
713
Route::get('/user', function () {
@@ -724,7 +724,7 @@ Alternatively, you may define a `withResponse` method within the resource itself
0 commit comments