Browse Source

fix resources

pull/4247/head
Taylor Otwell 1 year ago
parent
commit
73bfe08cee
1 changed files with 11 additions and 11 deletions
  1. 11
    11
      eloquent-resources.md

+ 11
- 11
eloquent-resources.md View File

@@ -21,7 +21,7 @@ When building an API, you may need a transformation layer that sits between your
21 21
 
22 22
 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:
23 23
 
24
-    php artisan make:resource UserResource
24
+    php artisan make:resource User
25 25
 
26 26
 #### Resource Collections
27 27
 
@@ -38,7 +38,7 @@ To create a resource collection, you should use the `--collection` flag when cre
38 38
 
39 39
 > {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.
40 40
 
41
-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` class:
41
+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:
42 42
 
43 43
     <?php
44 44
 
@@ -46,7 +46,7 @@ Before diving into all of the options available to you when writing resources, l
46 46
 
47 47
     use Illuminate\Http\Resources\Json\JsonResource;
48 48
 
49
-    class UserResource extends JsonResource
49
+    class User extends JsonResource
50 50
     {
51 51
         /**
52 52
          * Transform the resource into an array.
@@ -69,7 +69,7 @@ Before diving into all of the options available to you when writing resources, l
69 69
 Every resource class defines a `toArray` method which returns the array of attributes that should be converted to JSON when sending the response. Notice 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:
70 70
 
71 71
     use App\User;
72
-    use App\Http\Resources\UserResource;
72
+    use App\Http\Resources\User as UserResource;
73 73
 
74 74
     Route::get('/user', function () {
75 75
         return new UserResource(User::find(1));
@@ -80,7 +80,7 @@ Every resource class defines a `toArray` method which returns the array of attri
80 80
 If you are returning a collection of resources or a paginated response, you may use the `collection` method when creating the resource instance in your route or controller:
81 81
 
82 82
     use App\User;
83
-    use App\Http\Resources\UserResource;
83
+    use App\Http\Resources\User as UserResource;
84 84
 
85 85
     Route::get('/user', function () {
86 86
         return UserResource::collection(User::all());
@@ -139,7 +139,7 @@ In essence, resources are simple. They only need to transform a given model into
139 139
 
140 140
     use Illuminate\Http\Resources\Json\JsonResource;
141 141
 
142
-    class UserResource extends JsonResource
142
+    class User extends JsonResource
143 143
     {
144 144
         /**
145 145
          * Transform the resource into an array.
@@ -162,7 +162,7 @@ In essence, resources are simple. They only need to transform a given model into
162 162
 Once a resource has been defined, it may be returned directly from a route or controller:
163 163
 
164 164
     use App\User;
165
-    use App\Http\Resources\UserResource;
165
+    use App\Http\Resources\User as UserResource;
166 166
 
167 167
     Route::get('/user', function () {
168 168
         return new UserResource(User::find(1));
@@ -197,7 +197,7 @@ If you would like to include related resources in your response, you may add the
197 197
 While resources translate a single model into an array, resource collections translate a collection of models into an array. It is not absolutely necessary to define a resource collection class for each one of your model types since all resources provide a `collection` method to generate an "ad-hoc" resource collection on the fly:
198 198
 
199 199
     use App\User;
200
-    use App\Http\Resources\UserResource;
200
+    use App\Http\Resources\User as UserResource;
201 201
 
202 202
     Route::get('/user', function () {
203 203
         return UserResource::collection(User::all());
@@ -582,7 +582,7 @@ You may also add top-level data when constructing resource instances in your rou
582 582
 As you have already read, resources may be returned directly from routes and controllers:
583 583
 
584 584
     use App\User;
585
-    use App\Http\Resources\UserResource;
585
+    use App\Http\Resources\User as UserResource;
586 586
 
587 587
     Route::get('/user', function () {
588 588
         return new UserResource(User::find(1));
@@ -591,7 +591,7 @@ As you have already read, resources may be returned directly from routes and con
591 591
 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\Response` instance, allowing you full control of the response's headers:
592 592
 
593 593
     use App\User;
594
-    use App\Http\Resources\UserResource;
594
+    use App\Http\Resources\User as UserResource;
595 595
 
596 596
     Route::get('/user', function () {
597 597
         return (new UserResource(User::find(1)))
@@ -607,7 +607,7 @@ Alternatively, you may define a `withResponse` method within the resource itself
607 607
 
608 608
     use Illuminate\Http\Resources\Json\JsonResource;
609 609
 
610
-    class UserResource extends JsonResource
610
+    class User extends JsonResource
611 611
     {
612 612
         /**
613 613
          * Transform the resource into an array.

Loading…
Cancel
Save