Jelajahi Sumber

Adds ReadMe section for Resource Keys and camel casing to attribute tags

Georgi Morozov 4 tahun lalu
induk
melakukan
0328d95aca

+ 18 - 0
docs/documenting/documenting-endpoint-responses.md

@@ -253,6 +253,24 @@ Scribe will generate an instance (or instances) of the model and pass the model(
 .. Tip:: To understand how Scribe generates an instance of your model and how you can customize that, you should check out the section on `How model instances are generated`_.
 ```
 
+### Adding a Fractal Resource Key
+If your response data is nested within a Fractal Resource Key, you can specify it via an additional attribute in the `@transformerModel` tag.
+You can read more about Fractal Resource Keys on the [Fractal (The PHP League)](https://fractal.thephpleague.com/serializers/#jsonapiserializer) website.
+
+In addition to setting the resourceKey attribute, please make sure your Serializer is utilizing the resourceKey as well.
+
+
+```php
+/**
+ * @apiResource App\Resources\UserResource
+ * @apiResourceModel App\Models\User resourceKey=user
+ */
+public function showUser(User $user)
+{
+    return new UserResource($user);
+}
+```
+
 ### Paginating with transformers
 If your endpoint uses a paginator with the transformer, you can tell Scribe how to paginate via an additional tag, `@transformerPaginator`.
 

+ 2 - 3
src/Extracting/Strategies/Responses/UseTransformerTags.php

@@ -96,7 +96,6 @@ class UseTransformerTags extends Strategy
         }
 
         $response = response($fractal->createData($resource)->toJson());
-
         return [
             [
                 'status' => $statusCode ?: 200,
@@ -139,10 +138,10 @@ class UseTransformerTags extends Strategy
         $relations = [];
         $resourceKey = null;
         if ($modelTag) {
-            ['content' => $type, 'attributes' => $attributes] = a::parseIntoContentAndAttributes($modelTag->getContent(), ['states', 'with', 'resourcekey']);
+            ['content' => $type, 'attributes' => $attributes] = a::parseIntoContentAndAttributes($modelTag->getContent(), ['states', 'with', 'resourceKey']);
             $states = $attributes['states'] ? explode(',', $attributes['states']) : [];
             $relations = $attributes['with'] ? explode(',', $attributes['with']) : [];
-            $resourceKey = $attributes['resourcekey'] ?? null;
+            $resourceKey = $attributes['resourceKey'] ?? null;
         } else {
             $parameter = Arr::first($transformerMethod->getParameters());
             if ($parameter->hasType() && !$parameter->getType()->isBuiltin() && class_exists($parameter->getType()->getName())) {