{"id":368458,"date":"2024-05-21T03:43:07","date_gmt":"2024-05-21T03:43:07","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=368458"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=368458","title":{"rendered":"<span>\u041c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f JSON \u0440\u0435\u0441\u043f\u043e\u043d\u0441\u043e\u0432 \u0432 Laravel<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0447\u0435\u0440\u0430 \u044f <a href=\"https:\/\/habr.com\/ru\/articles\/794336\/\" rel=\"noopener noreferrer nofollow\">\u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043b \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u0438<\/a> \u043d\u0430 \u0442\u0435\u043c\u0443 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0440\u0435\u0441\u043f\u043e\u043d\u0441\u043e\u0432 \u0432 Laravel  \u0441 &#171;\u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0438\u043c\u0438&#187; \u0434\u0430\u043d\u043d\u044b\u043c\u0438. \u0422\u043e \u0435\u0441\u0442\u044c \u043a\u043e\u0433\u0434\u0430 \u0432 \u043e\u0442\u0432\u0435\u0442 \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u0434\u0430\u0442\u044c \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0447\u0438\u0441\u043b\u043e, \u0441\u0442\u0440\u043e\u043a\u0443, \u043c\u0430\u0441\u0441\u0438\u0432 \u0438\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442. \u041d\u043e \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0435\u0441\u043b\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043e \u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <a href=\"https:\/\/laravel.com\/docs\/eloquent-resources\" rel=\"noopener noreferrer nofollow\">Json Resource<\/a>? \u0418\u043b\u0438 \u0435\u0449\u0451 \u0431\u043e\u043b\u044c\u0448\u0435 &#8212; \u043d\u0443\u0436\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0445 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0435\u0439? \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f!<\/p>\n<p>\u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u0432\u0432\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u043b\u043e\u0434\u0438\u0442\u044c \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438, \u0431\u0443\u0434\u0435\u043c \u0432\u0441\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u043c \u0440\u0435\u0441\u0443\u0440\u0441\u0435 \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u0439 \u0436\u0435 \u0441\u0430\u043c\u043e\u0439 \u043b\u043e\u0433\u0438\u043a\u0435. \u0412\u043e\u0442 \u043e\u043d:<\/p>\n<pre><code class=\"php\">&lt;?php  use Illuminate\\Http\\Resources\\Json\\JsonResource;  class UserResource extends JsonResource {     public function toArray(Request $request): array     {         return [             'id' => $this->id,              'firstName' => $this->first_name,             'lastName' => $this->last_name,         ];     } }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u0432\u0435\u0440\u043d\u0451\u043c \u044d\u0442\u043e\u0442 \u0440\u0435\u0441\u0443\u0440\u0441 \u0438\u0437 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430, \u0442\u043e \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439:<\/p>\n<pre><code class=\"json\">{   \"data\": {     \"id\": 1,     \"firstName\": \"\u041d\u0438\u043d\u0430\",     \"lastName\": \"\u0414\u0440\u043e\u0437\u0434\u043e\u0432\u0430\"   } }<\/code><\/pre>\n<p>\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e, \u0432\u044b\u0432\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u0442\u0430\u043a\u0438\u043c:<\/p>\n<pre><code class=\"json\">{   \"data\": [     {       \"id\": 1,       \"firstName\": \"\u041d\u0438\u043d\u0430\",       \"lastName\": \"\u0414\u0440\u043e\u0437\u0434\u043e\u0432\u0430\"     }   ] }<\/code><\/pre>\n<p>\u0412\u0440\u043e\u0434\u0435 \u0432\u0441\u0451 \u0445\u043e\u0440\u043e\u0448\u043e, \u043d\u043e \u0432\u043e\u0442 \u043f\u0440\u0438\u043b\u0435\u0442\u0435\u043b\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u043e\u0442\u0432\u0435\u0442 <code>\"status\": \"OK\"<\/code>. \u0421\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0434\u0432\u0443\u043c\u044f \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438: \u043f\u0435\u0440\u0432\u044b\u0439 &#8212; \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430 <code>additional(['status' => 'OK'])<\/code> \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 \u0438\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043b\u0430\u0441\u0441\u0430:<\/p>\n<pre><code class=\"php\">&lt;?php  return UserResource::collection($user)->additional(['status' => 'OK']);<\/code><\/pre>\n<pre><code class=\"php\">&lt;?php  class UserResource extends Resource {     public $with = [         'status' => 'OK'     ];      public function toArray(Request $request): array     {         return [             'id' => $this->id,              'firstName' => $this->first_name,             'lastName' => $this->last_name,         ];     } }<\/code><\/pre>\n<p>\u041d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043e\u0431\u044a\u0435\u043a\u0442 <code>{\"data\": ..., \"status\": \"OK\"}<\/code>.<\/p>\n<p>\u0412\u0440\u043e\u0434\u0435 \u0441\u043f\u0440\u0430\u0432\u0438\u043b\u0438\u0441\u044c. \u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0440\u0430\u0437\u043d\u0430\u0440\u044f\u0434\u043a\u0430 \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f\u0445 \u043e\u0431\u0435\u0440\u043d\u0443\u0442\u044c \u043e\u0442\u0432\u0435\u0442 \u0432 \u043a\u0430\u043a\u043e\u0439-\u043b\u0438\u0431\u043e \u043a\u043b\u044e\u0447, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, &#171;users&#187;. \u0417\u0430\u0447\u0435\u043c? \u0427\u0442\u043e\u0431\u044b \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0449\u0451 \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043e\u0442\u0432\u0435\u0442. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, JSON \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"json\">\/\/ \u0434\u043b\u044f \u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 {   \"data\": {     \"id\": 1,     \"firstName\": \"\u041d\u0438\u043d\u0430\",     \"lastName\": \"\u0414\u0440\u043e\u0437\u0434\u043e\u0432\u0430\"   },   \"status\": \"OK\" }  \/\/ \u0434\u043b\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 {   \"data\": {     \"users\": [       {         \"id\": 1,         \"firstName\": \"\u041d\u0438\u043d\u0430\",        \"lastName\": \"\u0414\u0440\u043e\u0437\u0434\u043e\u0432\u0430\"       }     ]   },   \"status\": \"OK\" }<\/code><\/pre>\n<p>\u041d\u043e \u043a\u0430\u043a \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c? \u0418 \u043a\u0430\u043a \u043d\u0435 \u0437\u0430\u0431\u044b\u0442\u044c \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u0442 \u0441\u0430\u043c\u044b\u0439 \u043a\u043b\u044e\u0447 &#171;users&#187;? \u0427\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0435\u0441\u043b\u0438 \u0437\u0430\u0431\u044b\u0442\u044c? \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c\u0441\u044f.<\/p>\n<p>\u041d\u0430 \u0441\u043b\u0443\u0447\u0430\u0439 &#171;\u0437\u0430\u0431\u044b\u0442\u044b\u0445 \u043a\u043b\u044e\u0447\u0435\u0439&#187; \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u0434\u0432\u0443\u043c\u044f \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438 &#8212; \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0438\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0433\u0438\u044e Laravel. \u041f\u0435\u0440\u0432\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0442\u0438\u043f\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>protected static string $dataKey;<\/code>. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u0430\u043c PHP \u0432\u0435\u0440\u043d\u0451\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 \u043f\u0440\u0438 \u043f\u043e\u043f\u044b\u0442\u043a\u0435 \u0447\u0442\u0435\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u0431\u0435\u0437 \u0435\u0433\u043e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u041d\u043e \u044d\u0442\u043e \u0441\u0442\u0440\u0435\u043b\u044f\u0435\u0442 \u0432 \u043d\u043e\u0433\u0443 \u0432 \u0442\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u0432 \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u0445 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0435\u0433\u043e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0438\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432\u0442\u043e\u0440\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 &#8212; \u043c\u0430\u0433\u0438\u044e.<\/p>\n<p>Laravel \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c JSON \u0440\u0435\u0441\u0443\u0440\u0441 \u043a\u0430\u043a \u0434\u043b\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u043d\u0430\u0431\u043e\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u0442\u0430\u043a \u0438 \u0434\u043b\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438, \u0432\u044b\u0437\u044b\u0432\u0430\u044f \u043c\u0435\u0442\u043e\u0434\u044b <code>make<\/code> \u0438 <code>collection<\/code> \u0443 \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u041f\u043e\u0434\u043a\u0430\u043f\u043e\u0442\u043d\u0430\u044f \u043c\u0430\u0433\u0438\u044f \u0443\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u0442\u0430\u043a, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0440\u044f\u0434\u043e\u043c \u0441 \u043a\u043b\u0430\u0441\u0441\u043e\u043c \u043b\u0435\u0436\u0438\u0442 \u0442\u0430\u043a\u043e\u0439 \u0436\u0435 \u043f\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044e, \u043d\u043e \u0441 \u0441\u0443\u0444\u0444\u0438\u043a\u0441\u043e\u043c <code>Collection<\/code>, \u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u0437\u044f\u0442 \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u043d. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>UserResourceCollection<\/code> \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <code>UserResource::collection()<\/code>. \u041c\u044b \u0443\u0447\u0442\u0451\u043c \u044d\u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u0438 \u0432\u043d\u0430\u0447\u0430\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0431\u0449\u0438\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f\u043c\u0438:<\/p>\n<pre><code class=\"php\">&lt;?php  namespace App\\Http\\Resources;  use Illuminate\\Http\\Request; use Illuminate\\Http\\Resources\\Json\\AnonymousResourceCollection; use Illuminate\\Support\\Str;  final class Collection extends AnonymousResourceCollection {     public function __construct(         mixed $resource,         string $collects,         protected ?string $dataKey     ) {         $this->dataKey ??= $this->resolveDataKey($collects);          parent::__construct($resource, $collects);     }      public function toArray(Request $request): array     {         return [             'data' => [                 $this->dataKey => $this->collection,             ],             'status' => 'OK',         ];     }      protected function resolveDataKey(string $collection): string     {         return Str::of($collection)             ->afterLast('\\\\')             ->beforeLast('Resource')             ->snake()             ->plural()             ->toString();     } } <\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0441\u0430\u043c\u043e\u0433\u043e \u0440\u0435\u0441\u0443\u0440\u0441\u0430:<\/p>\n<pre><code class=\"php\">&lt;?php  namespace App\\Http\\Resources;  use Illuminate\\Http\\Resources\\Json\\JsonResource;  class Resource extends JsonResource {     protected static ?string $dataKey = null;      public $with = [         'status' => 'OK',     ];      public static function collection($resource): Collection     {         return new Collection($resource, static::class, static::$dataKey);     } } <\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043a\u043b\u0430\u0441\u0441 <code>Collection<\/code> \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u0441\u044b\u043b\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0442\u043e\u0442 \u0447\u0442\u043e \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438 \u0440\u0430\u043d\u043d\u0435\u0435, \u0430 \u043d\u0435 \u043d\u0430 \u043e\u0434\u043d\u043e\u0438\u043c\u0451\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 Laravel.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441 \u0440\u0435\u0441\u0443\u0440\u0441\u0430 \u0434\u043b\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f. \u041c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0441\u0443\u0440\u0441, \u0438\u0437\u043c\u0435\u043d\u0438\u0432 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435:<\/p>\n<pre><code class=\"php\">&lt;?php  use App\\Http\\Resources\\Resource;  class UserResource extends Resource {     protected static ?string $dataKey = 'users';        public function toArray(Request $request): array     {         return [             'id' => $this->id,              'firstName' => $this->first_name,             'lastName' => $this->last_name,         ];     } }<\/code><\/pre>\n<p>\u0412\u043e\u0442 \u0438 \u0432\u0441\u0451. \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <code>UserResource::make($user)<\/code> \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c &#171;\u043f\u0440\u043e\u0441\u0442\u043e\u0439&#187; \u043e\u0431\u044a\u0435\u043a\u0442 \u0431\u0435\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u0438, \u0430 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <code>UserResource::collection($users)<\/code> &#8212; \u043a\u043b\u044e\u0447 &#171;users&#187; \u0432\u043d\u0443\u0442\u0440\u0438 &#171;data&#187;, \u043a\u0430\u043a \u0438 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u043e\u0441\u044c \u0432\u044b\u0448\u0435.<\/p>\n<h3>\u041a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442?<\/h3>\n<p>\u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u0430\u044f \u043c\u0435\u0445\u0430\u043d\u0438\u043a\u0430 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e. \u041f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f &#171;\u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e&#187; \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0445\u043e\u0442\u044c \u0447\u0435\u0440\u0435\u0437 <code>new UserResource($user)<\/code>, \u0445\u043e\u0442\u044c \u0447\u0435\u0440\u0435\u0437 <code>UserResource::make($user)<\/code>, Laravel \u0441\u043e\u0437\u0434\u0430\u0441\u0442 \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0438 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u0435\u0439. \u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0447\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0435\u0442 &#8212; \u044d\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442 <code>'status' => 'OK'<\/code> \u0432 \u043e\u0442\u0432\u0435\u0442, \u0430 \u0432\u043e\u0442 \u043f\u0440\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0441\u044f \u043d\u0430\u0448 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0438, \u0435\u0441\u043b\u0438 \u0432 \u0440\u0435\u0441\u0443\u0440\u0441\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>$dataKey<\/code>, \u0442\u043e \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 JSON \u043e\u0431\u044a\u0435\u043a\u0442 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0438\u043d\u0430\u0447\u0435 \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 <code>resolveDataKey<\/code> \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u044c\u043c\u0451\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u0443\u044e \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u043a\u043b\u0430\u0441\u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u0440\u0435\u0441\u0443\u0440\u0441\u0430, \u0434\u0430\u043b\u0435\u0435 \u043e\u0442\u0441\u0435\u0438\u0442 \u0432\u0441\u0451 \u0447\u0442\u043e \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u0441\u043b\u0435\u0448\u0435\u043c, \u0434\u0430\u043b\u0435\u0435 \u0432\u043e\u0437\u044c\u043c\u0451\u0442 \u0432\u0441\u0451 \u0447\u0442\u043e \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434 \u0441\u043b\u043e\u0432\u043e\u043c <code>Resource<\/code>, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442 \u0432 <code>snake_case<\/code> \u0438 \u0441\u0434\u0435\u043b\u0430\u0435\u0442 \u0432\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u043c \u0447\u0438\u0441\u043b\u0435.<\/p>\n<p>\u0417\u0432\u0443\u0447\u0438\u0442 \u0441\u043b\u043e\u0436\u043d\u043e, \u0430 \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0432\u0441\u0451 \u043f\u0440\u043e\u0449\u0435 \u043d\u0435\u043a\u0443\u0434\u0430. \u0422\u0430\u043a \u043a\u0430\u043a \u043c\u044b \u044f\u0432\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u043b\u0438 \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 \u044e\u0437\u0435\u0440\u043e\u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440, \u0442\u043e \u0434\u0430\u043d\u043d\u0430\u044f \u043c\u0435\u0445\u0430\u043d\u0438\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u043d\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0438, \u0447\u0442\u043e\u0431\u044b \u0434\u043e\u043b\u0433\u043e \u043d\u0435 \u0434\u0443\u043c\u0430\u0442\u044c \u043d\u0430\u0434 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c, \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0431\u0430\u043d\u0430\u043b\u044c\u043d\u043e:<\/p>\n<pre><code class=\"php\">&lt;?php  namespace App\\Http\\Resources;  class MyWorkResource extends Resource {        public function toArray(Request $request): array     {         return [             'id' => $this->id,             'title' => $this->title,         ];     } }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0438\u043c\u0435\u043d\u0438 \u043a\u043b\u044e\u0447\u0430 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u044b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0448\u0430\u0433\u0438:<\/p>\n<pre><code>App\\Http\\Resources\\MyWorkResource MyWorkResource MyWork my_work my_works<\/code><\/pre>\n<p>\u041d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0438\u043c\u044f \u043a\u043b\u044e\u0447\u0430 &#8212; <code>my_works<\/code>:<\/p>\n<pre><code class=\"json\">{   \"data\": {     \"my_works\": []   },   \"status\": \"OK\" }<\/code><\/pre>\n<p>\u0412\u0441\u0451 \u043f\u0440\u043e\u0449\u0435 \u043d\u0435\u043a\u0443\u0434\u0430. \u0418 \u043a\u0440\u0430\u0441\u0438\u0432\u043e! ?<\/p>\n<h2>\u0411\u043e\u043d\u0443\u0441<\/h2>\n<p>\u0414\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0447\u0430\u0441\u0442\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043c\u0435\u043d\u044f\u044e\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0445 JSON \u043a\u043b\u044e\u0447\u0435\u0439 \u0441\u043e <code>snake_case<\/code> \u043d\u0430 <code>camelCase<\/code> \u0438, \u0437\u0430\u0447\u0430\u0441\u0442\u0443\u044e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0438. \u041e\u0434\u0438\u043d \u0438\u0437 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u043b, \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b \u0436\u0443\u0442\u043a\u043e:<\/p>\n<pre><code class=\"php\">&lt;?php  class PaginatorCamelCaseResource extends PaginatorResource {     public function toArray($request): array     {         return collect(parent::toArray($request))             ->put('total', $this->total())             ->mapWithKeys(                 fn (mixed $item, string $key) => [Str::camel($key) => $item]             )->toArray();     } }<\/code><\/pre>\n<p>\u041d\u043e \u043a\u0430\u043a \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u043e-\u0447\u0435\u043b\u043e\u0432\u0435\u0447\u0435\u0441\u043a\u0438? &#8212; \u0441\u043f\u0440\u043e\u0441\u0438\u0442\u0435 \u0432\u044b. \u041b\u0435\u0433\u043a\u043e! \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u0441\u044f \u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u043d\u0430\u043c\u0438 \u043a\u043b\u0430\u0441\u0441 <code>Collection<\/code> \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u043d\u0435\u0433\u043e \u043d\u0435\u0445\u0438\u0442\u0440\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 <code>paginationInformation<\/code> \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u044b\u043c:<\/p>\n<pre><code class=\"php\">&lt;?php  public function paginationInformation(Request $request, array $paginated, array $default): array {     return [         'pagination' => [             'total' => $default['meta']['total'] ?? 0,             'perPage' => $default['meta']['per_page'] ?? 0,             'currentPage' => $default['meta']['current_page'] ?? 0,             'lastPage' => $default['meta']['last_page'] ?? 0,         ],     ]; }<\/code><\/pre>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0438\u0437 \u0432\u0441\u0435\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0438 \u043d\u0443\u0436\u043d\u044b \u043b\u0438\u0448\u044c \u043d\u043e\u043c\u0435\u0440\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446 \u0438 \u043e\u0431\u0449\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u043d\u043e \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043b\u0435\u0433\u043a\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a. \u041d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0442\u0430\u043a\u043e\u0439 \u043f\u0440\u0438\u0451\u043c \u0432\u0435\u0440\u043d\u0451\u0442 \u043e\u0431\u044a\u0435\u043a\u0442:<\/p>\n<pre><code class=\"php\">&lt;?php  $users = User::paginate();  return UserResource::collection($users);<\/code><\/pre>\n<pre><code class=\"json\">{   \"data\": {     \"users\": [       {         \"id\": 1,         \"firstName\": \"\u041d\u0438\u043d\u0430\",        \"lastName\": \"\u0414\u0440\u043e\u0437\u0434\u043e\u0432\u0430\"       },       ...     ]   },   \"pagination\": {     \"total\": 27,     \"perPage\": 5,     \"currentPage\": 1,     \"lastPage\": 6   }   \"status\": \"OK\" }<\/code><\/pre>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u0440\u0430\u0432\u043d\u0438\u043c \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u0431\u044b\u043b\u043e:<\/p>\n<pre><code class=\"json\">{     \"data\": {         \"users\": [             {                 \"id\": 1,                 \"firstName\": \"\u041d\u0438\u043d\u0430\",                 \"lastName\": \"\u0414\u0440\u043e\u0437\u0434\u043e\u0432\u0430\"             },             ...         ]     },     \"status\": \"OK\",     \"links\": {         \"first\": \"http:\\\/\\\/127.0.0.1:8000\\\/api?page=1\",         \"last\": \"http:\\\/\\\/127.0.0.1:8000\\\/api?page=6\",         \"prev\": null,         \"next\": \"http:\\\/\\\/127.0.0.1:8000\\\/api?page=2\"     },     \"meta\": {         \"current_page\": 1,         \"from\": 1,         \"last_page\": 6,         \"links\": [             {                 \"url\": null,                 \"label\": \"&amp;laquo; Previous\",                 \"active\": false             },             {                 \"url\": \"http:\\\/\\\/127.0.0.1:8000\\\/api?page=1\",                 \"label\": \"1\",                 \"active\": true             },             {                 \"url\": \"http:\\\/\\\/127.0.0.1:8000\\\/api?page=2\",                 \"label\": \"2\",                 \"active\": false             },             {                 \"url\": \"http:\\\/\\\/127.0.0.1:8000\\\/api?page=3\",                 \"label\": \"3\",                 \"active\": false             },             {                 \"url\": \"http:\\\/\\\/127.0.0.1:8000\\\/api?page=4\",                 \"label\": \"4\",                 \"active\": false             },             {                 \"url\": \"http:\\\/\\\/127.0.0.1:8000\\\/api?page=5\",                 \"label\": \"5\",                 \"active\": false             },             {                 \"url\": \"http:\\\/\\\/127.0.0.1:8000\\\/api?page=6\",                 \"label\": \"6\",                 \"active\": false             },             {                 \"url\": \"http:\\\/\\\/127.0.0.1:8000\\\/api?page=2\",                 \"label\": \"Next &amp;raquo;\",                 \"active\": false             }         ],         \"path\": \"http:\\\/\\\/127.0.0.1:8000\\\/api\",         \"per_page\": 5,         \"to\": 5,         \"total\": 27     } }<\/code><\/pre>\n<p>\u041f\u043b\u044e\u0441\u044b \u0438 \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u0430 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b.<\/p>\n<h2>\u041e\u0434\u0438\u043d \u043a\u043b\u0430\u0441\u0441, \u0447\u0442\u043e \u043f\u0440\u0430\u0432\u0438\u0442 \u0432\u0441\u0435\u043c\u0438<\/h2>\n<p>\u041d\u0435 \u0437\u0440\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u0441\u0442\u0430\u0442\u044c\u0438 \u044f \u0443\u043f\u043e\u043c\u044f\u043d\u0443\u043b \u043e <a href=\"https:\/\/habr.com\/ru\/articles\/794336\/\" rel=\"noopener noreferrer nofollow\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u043f\u043e\u0441\u0442\u0435<\/a>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0435\u0439\u0441 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043a\u043e\u0434\u0430\u043c\u0438 \u043e\u0442\u0432\u0435\u0442\u043e\u0432. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u0435\u0433\u043e \u0438 \u0441\u044e\u0434\u0430.<\/p>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <code>toJson<\/code> \u0443 \u0440\u0435\u0441\u0443\u0440\u0441\u0430 \u043f\u0435\u0440\u0435\u0434\u0430\u0432 \u0432 \u0435\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u043d\u0443\u0436\u043d\u044b\u0439 \u0444\u043b\u0430\u0433, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>JSON_PRETTY_PRINT<\/code>, \u043d\u043e \u044d\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442 \u0434\u0443\u0431\u043b\u044f\u0436 \u043a\u043e\u0434\u0430 \u043f\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u043f\u043e\u0439\u0434\u0451\u043c \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0438\u043c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c &#8212; \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435\u043c.<\/p>\n<p>\u0414\u043e\u043f\u043e\u043b\u043d\u0438\u043c \u043d\u0430\u0448 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 <code>Response<\/code> \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0432 \u043f\u0443\u0431\u043b\u0438\u0447\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 <code>jsonOptions<\/code>:<\/p>\n<pre><code class=\"php\">&lt;?php  namespace App\\Http\\Resources;  use Illuminate\\Http\\Resources\\Json\\JsonResource;  abstract class Resource extends JsonResource {     protected static ?string $dataKey = null;      public $with = [         'status' => 'OK',     ];      public static function collection($resource): Collection     {         return new Collection($resource, static::class, static::$dataKey);     }      public function jsonOptions(): int     {         return JSON_UNESCAPED_UNICODE ^ JSON_PRETTY_PRINT;     } }<\/code><\/pre>\n<p>\u0412\u043e\u0442 \u0438 \u0432\u0441\u0451. \u041d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043a\u0440\u0430\u0441\u0438\u0432\u043e \u043e\u0442\u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 JSON:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e43\/4b4\/52e\/e434b452ed9af4a608f9bc870bcc7d7c.png\" width=\"367\" height=\"204\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e43\/4b4\/52e\/e434b452ed9af4a608f9bc870bcc7d7c.png\"\/><\/figure>\n<p>\u041a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u0442\u043e\u0442 \u0436\u0435 \u0441\u0430\u043c\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0432 \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u0439 \u043a\u043b\u0430\u0441\u0441 <code>Collection<\/code> \u0438 \u0432\u0443\u0430\u043b\u044f:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/314\/b21\/d7c\/314b21d7c4411c7f3ee73a16d00b09c5.png\" width=\"366\" height=\"649\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/314\/b21\/d7c\/314b21d7c4411c7f3ee73a16d00b09c5.png\"\/><\/figure>\n<h2>Laravel Idea + artisan make:resource<\/h2>\n<p>\u041d\u043e \u043a\u0430\u043a \u0436\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0448 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u0440\u0435\u0441\u0443\u0440\u0441 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u043b\u0441\u044f \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0431\u0443\u0434\u044c \u0442\u043e \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u043d\u0441\u043e\u043b\u044c\u043d\u0443\u044e \u043a\u043e\u043c\u0430\u043d\u0434\u0443 <code>php artisan make:resource<\/code> \u0438\u043b\u0438 \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u0445\u043e\u0442\u043a\u0435\u044f \u0432 \u043f\u043b\u0430\u0433\u0438\u043d\u0435 <a href=\"https:\/\/laravel-idea.com\/\" rel=\"noopener noreferrer nofollow\">Laravel Idea<\/a> \u0434\u043b\u044f PhpStorm? \u0418 \u0437\u0434\u0435\u0441\u044c \u043d\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c!<\/p>\n<h3>Laravel Templates<\/h3>\n<p>\u0414\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u0448\u0430\u0431\u043b\u043e\u043d \u0434\u043b\u044f \u0444\u0430\u0439\u043b\u043e\u0432, \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0435\u043c\u044b\u0445 \u043a\u043e\u043d\u0441\u043e\u043b\u044c\u043d\u044b\u043c\u0438 \u043a\u043e\u043c\u0430\u043d\u0434\u0430\u043c\u0438 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430 \u043d\u0443\u0436\u043d\u043e \u0438\u0445 \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u043e\u0439:<\/p>\n<pre><code class=\"bash\">php artisan stub:publish<\/code><\/pre>\n<p>Laravel \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u0443\u0435\u0442 \u0432\u0441\u0435 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0435 \u0444\u0430\u0439\u043b\u044b \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u0432 \u0432 \u043f\u0430\u043f\u043a\u0443 <code>stubs<\/code> \u0432 \u043a\u043e\u0440\u043d\u0435 \u0412\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u041d\u0430\u0441 \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u0435\u0442 \u0444\u0430\u0439\u043b <code>stubs\/resource.stub<\/code>. \u0412\u0441\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0443\u0434\u0430\u043b\u0438\u0442\u044c, \u0435\u0441\u043b\u0438 \u043d\u0435 \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u0435\u0442\u0435 \u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u043c \u0448\u0430\u0431\u043b\u043e\u043d \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u0438\u0434\u0443:<\/p>\n<pre><code class=\"php\">&lt;?php  namespace {{ namespace }};  use Illuminate\\Http\\Request; use App\\Http\\Resources\\Resource;  class {{ class }} extends Resource {     public function toArray(Request $request): array     {         \/\/     } } <\/code><\/pre>\n<p>\u0418 \u0432\u0441\u0451. \u041f\u043e\u0441\u043b\u0435 \u0432\u044b\u0437\u043e\u0432\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0440\u0435\u0441\u0443\u0440\u0441\u0430 \u043a\u043e\u043d\u0441\u043e\u043b\u044c\u043d\u043e\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u043e\u0439 \u043a\u043b\u0430\u0441\u0441 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043d\u0430\u043c\u0438.<\/p>\n<h3>Laravel Idea<\/h3>\n<p>\u0418\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u0442\u0430\u043a\u0436\u0435 \u043b\u0435\u0433\u043a\u043e. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0439\u0434\u0438\u0442\u0435 \u0432 \u043f\u0443\u043d\u043a\u0442 \u043c\u0435\u043d\u044e <code>Laravel > Laravel Idea Settings...<\/code> (\u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0447\u0435\u0440\u0435\u0437 \u043f\u0443\u043d\u043a\u0442 \u043c\u0435\u043d\u044e <code>File > Settings<\/code> \u0438 \u0434\u0430\u043b\u044c\u0448\u0435 <code>File | Settings | Languages &amp; Frameworks | Laravel Idea | General<\/code>), \u0437\u0430\u0442\u0435\u043c \u0432 \u043f\u043e\u0434\u043c\u0435\u043d\u044e <code>Code Generation > Standard Generations<\/code>, \u043d\u0430\u0439\u0434\u0438\u0442\u0435 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 <code>Create Json Resource<\/code> \u0438 \u0432 \u043f\u043e\u043b\u0435 <code>Base class<\/code> \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u0435 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u043d\u0430 \u0441\u0432\u043e\u0439. \u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e <code>App\\Http\\Resources\\Resource<\/code>.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/a96\/04b\/fee\/a9604bfee3f63d038c0f047af9661645.png\" width=\"336\" height=\"306\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a96\/04b\/fee\/a9604bfee3f63d038c0f047af9661645.png\"\/><\/figure>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/69d\/64c\/b94\/69d64cb946ecda58130b94a361a0251c.png\" width=\"1172\" height=\"736\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/69d\/64c\/b94\/69d64cb946ecda58130b94a361a0251c.png\"\/><\/figure>\n<p>\u0418 \u0432\u0441\u0451. \u0422\u0435\u043f\u0435\u0440\u044c \u0432\u0441\u0435 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0435\u043c\u044b\u0435 \u043b\u044e\u0431\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u0431\u0443\u0434\u0443\u0442 \u0438\u043c\u0435\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/794388\/\"> https:\/\/habr.com\/ru\/articles\/794388\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0447\u0435\u0440\u0430 \u044f <a href=\"https:\/\/habr.com\/ru\/articles\/794336\/\" rel=\"noopener noreferrer nofollow\">\u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043b \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u0438<\/a> \u043d\u0430 \u0442\u0435\u043c\u0443 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0440\u0435\u0441\u043f\u043e\u043d\u0441\u043e\u0432 \u0432 Laravel  \u0441 &#171;\u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0438\u043c\u0438&#187; \u0434\u0430\u043d\u043d\u044b\u043c\u0438. \u0422\u043e \u0435\u0441\u0442\u044c \u043a\u043e\u0433\u0434\u0430 \u0432 \u043e\u0442\u0432\u0435\u0442 \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u0434\u0430\u0442\u044c \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0447\u0438\u0441\u043b\u043e, \u0441\u0442\u0440\u043e\u043a\u0443, \u043c\u0430\u0441\u0441\u0438\u0432 \u0438\u043b\u0438 \u043e\u0431\u044a\u0435\u043a\u0442. \u041d\u043e \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0435\u0441\u043b\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043e \u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <a href=\"https:\/\/laravel.com\/docs\/eloquent-resources\" rel=\"noopener noreferrer nofollow\">Json Resource<\/a>? \u0418\u043b\u0438 \u0435\u0449\u0451 \u0431\u043e\u043b\u044c\u0448\u0435 &#8212; \u043d\u0443\u0436\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0445 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0435\u0439? \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f!<\/p>\n<p>\u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u0432\u0432\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u043b\u043e\u0434\u0438\u0442\u044c \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438, \u0431\u0443\u0434\u0435\u043c \u0432\u0441\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u043c \u0440\u0435\u0441\u0443\u0440\u0441\u0435 \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u0439 \u0436\u0435 \u0441\u0430\u043c\u043e\u0439 \u043b\u043e\u0433\u0438\u043a\u0435. \u0412\u043e\u0442 \u043e\u043d:<\/p>\n<pre><code class=\"php\">&lt;?php  use Illuminate\\Http\\Resources\\Json\\JsonResource;  class UserResource extends JsonResource {     public function toArray(Request $request): array     {         return [             'id' => $this->id,              'firstName' => $this->first_name,             'lastName' => $this->last_name,         ];     } }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043c\u044b \u0432\u0435\u0440\u043d\u0451\u043c \u044d\u0442\u043e\u0442 \u0440\u0435\u0441\u0443\u0440\u0441 \u0438\u0437 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430, \u0442\u043e \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439:<\/p>\n<pre><code class=\"json\">{   \"data\": {     \"id\": 1,     \"firstName\": \"\u041d\u0438\u043d\u0430\",     \"lastName\": \"\u0414\u0440\u043e\u0437\u0434\u043e\u0432\u0430\"   } }<\/code><\/pre>\n<p>\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e, \u0432\u044b\u0432\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u0442\u0430\u043a\u0438\u043c:<\/p>\n<pre><code class=\"json\">{   \"data\": [     {       \"id\": 1,       \"firstName\": \"\u041d\u0438\u043d\u0430\",       \"lastName\": \"\u0414\u0440\u043e\u0437\u0434\u043e\u0432\u0430\"     }   ] }<\/code><\/pre>\n<p>\u0412\u0440\u043e\u0434\u0435 \u0432\u0441\u0451 \u0445\u043e\u0440\u043e\u0448\u043e, \u043d\u043e \u0432\u043e\u0442 \u043f\u0440\u0438\u043b\u0435\u0442\u0435\u043b\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u043e\u0442\u0432\u0435\u0442 <code>\"status\": \"OK\"<\/code>. \u0421\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0434\u0432\u0443\u043c\u044f \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438: \u043f\u0435\u0440\u0432\u044b\u0439 &#8212; \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u0430 <code>additional(['status' => 'OK'])<\/code> \u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 \u0438\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043b\u0430\u0441\u0441\u0430:<\/p>\n<pre><code class=\"php\">&lt;?php  return UserResource::collection($user)->additional(['status' => 'OK']);<\/code><\/pre>\n<pre><code class=\"php\">&lt;?php  class UserResource extends Resource {     public $with = [         'status' => 'OK'     ];      public function toArray(Request $request): array     {         return [             'id' => $this->id,              'firstName' => $this->first_name,             'lastName' => $this->last_name,         ];     } }<\/code><\/pre>\n<p>\u041d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043e\u0431\u044a\u0435\u043a\u0442 <code>{\"data\": ..., \"status\": \"OK\"}<\/code>.<\/p>\n<p>\u0412\u0440\u043e\u0434\u0435 \u0441\u043f\u0440\u0430\u0432\u0438\u043b\u0438\u0441\u044c. \u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0440\u0430\u0437\u043d\u0430\u0440\u044f\u0434\u043a\u0430 \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f\u0445 \u043e\u0431\u0435\u0440\u043d\u0443\u0442\u044c \u043e\u0442\u0432\u0435\u0442 \u0432 \u043a\u0430\u043a\u043e\u0439-\u043b\u0438\u0431\u043e \u043a\u043b\u044e\u0447, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, &#171;users&#187;. \u0417\u0430\u0447\u0435\u043c? \u0427\u0442\u043e\u0431\u044b \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0449\u0451 \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043e\u0442\u0432\u0435\u0442. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, JSON \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"json\">\/\/ \u0434\u043b\u044f \u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 {   \"data\": {     \"id\": 1,     \"firstName\": \"\u041d\u0438\u043d\u0430\",     \"lastName\": \"\u0414\u0440\u043e\u0437\u0434\u043e\u0432\u0430\"   },   \"status\": \"OK\" }  \/\/ \u0434\u043b\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 {   \"data\": {     \"users\": [       {         \"id\": 1,         \"firstName\": \"\u041d\u0438\u043d\u0430\",        \"lastName\": \"\u0414\u0440\u043e\u0437\u0434\u043e\u0432\u0430\"       }     ]   },   \"status\": \"OK\" }<\/code><\/pre>\n<p>\u041d\u043e \u043a\u0430\u043a \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c? \u0418 \u043a\u0430\u043a \u043d\u0435 \u0437\u0430\u0431\u044b\u0442\u044c \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u0442 \u0441\u0430\u043c\u044b\u0439 \u043a\u043b\u044e\u0447 &#171;users&#187;? \u0427\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0435\u0441\u043b\u0438 \u0437\u0430\u0431\u044b\u0442\u044c? \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c\u0441\u044f.<\/p>\n<p>\u041d\u0430 \u0441\u043b\u0443\u0447\u0430\u0439 &#171;\u0437\u0430\u0431\u044b\u0442\u044b\u0445 \u043a\u043b\u044e\u0447\u0435\u0439&#187; \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u0434\u0432\u0443\u043c\u044f \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438 &#8212; \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0438\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0433\u0438\u044e Laravel. \u041f\u0435\u0440\u0432\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0442\u0438\u043f\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>protected static string $dataKey;<\/code>. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u0430\u043c PHP \u0432\u0435\u0440\u043d\u0451\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 \u043f\u0440\u0438 \u043f\u043e\u043f\u044b\u0442\u043a\u0435 \u0447\u0442\u0435\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430 \u0431\u0435\u0437 \u0435\u0433\u043e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438. \u041d\u043e \u044d\u0442\u043e \u0441\u0442\u0440\u0435\u043b\u044f\u0435\u0442 \u0432 \u043d\u043e\u0433\u0443 \u0432 \u0442\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u0432 \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u0445 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0435\u0433\u043e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0438\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432\u0442\u043e\u0440\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 &#8212; \u043c\u0430\u0433\u0438\u044e.<\/p>\n<p>Laravel \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c JSON \u0440\u0435\u0441\u0443\u0440\u0441 \u043a\u0430\u043a \u0434\u043b\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u043d\u0430\u0431\u043e\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u0442\u0430\u043a \u0438 \u0434\u043b\u044f \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438, \u0432\u044b\u0437\u044b\u0432\u0430\u044f \u043c\u0435\u0442\u043e\u0434\u044b <code>make<\/code> \u0438 <code>collection<\/code> \u0443 \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u041f\u043e\u0434\u043a\u0430\u043f\u043e\u0442\u043d\u0430\u044f \u043c\u0430\u0433\u0438\u044f \u0443\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u0442\u0430\u043a, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0440\u044f\u0434\u043e\u043c \u0441 \u043a\u043b\u0430\u0441\u0441\u043e\u043c \u043b\u0435\u0436\u0438\u0442 \u0442\u0430\u043a\u043e\u0439 \u0436\u0435 \u043f\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044e, \u043d\u043e \u0441 \u0441\u0443\u0444\u0444\u0438\u043a\u0441\u043e\u043c <code>Collection<\/code>, \u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u0437\u044f\u0442 \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u043d. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>UserResourceCollection<\/code> \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <code>UserResource::collection()<\/code>. \u041c\u044b \u0443\u0447\u0442\u0451\u043c \u044d\u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u0438 \u0432\u043d\u0430\u0447\u0430\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0431\u0449\u0438\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f\u043c\u0438:<\/p>\n<pre><code class=\"php\">&lt;?php  namespace App\\Http\\Resources;  use Illuminate\\Http\\Request; use Illuminate\\Http\\Resources\\Json\\AnonymousResourceCollection; use Illuminate\\Support\\Str;  final class Collection extends AnonymousResourceCollection {     public function __construct(         mixed $resource,         string $collects,         protected ?string $dataKey     ) {         $this->dataKey ??= $this->resolveDataKey($collects);          parent::__construct($resource, $collects);     }      public function toArray(Request $request): array     {         return [             'data' => [                 $this->dataKey => $this->collection,             ],             'status' => 'OK',         ];     }      protected function resolveDataKey(string $collection): string     {         return Str::of($collection)             ->afterLast('\\\\')             ->beforeLast('Resource')             ->snake()             ->plural()             ->toString();     } } <\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0431\u0430\u0437\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0441\u0430\u043c\u043e\u0433\u043e \u0440\u0435\u0441\u0443\u0440\u0441\u0430:<\/p>\n<pre><code class=\"php\">&lt;?php  namespace App\\Http\\Resources;  use Illuminate\\Http\\Resources\\Json\\JsonResource;  class Resource extends JsonResource {     protected static ?string $dataKey = null;      public $with = [         'status' => 'OK',     ];      public static function collection($resource): Collection     {         return new Collection($resource, static::class, static::$dataKey);     } } <\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043a\u043b\u0430\u0441\u0441 <code>Collection<\/code> \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u0441\u044b\u043b\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0442\u043e\u0442 \u0447\u0442\u043e \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438 \u0440\u0430\u043d\u043d\u0435\u0435, \u0430 \u043d\u0435 \u043d\u0430 \u043e\u0434\u043d\u043e\u0438\u043c\u0451\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 Laravel.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441 \u0440\u0435\u0441\u0443\u0440\u0441\u0430 \u0434\u043b\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f. \u041c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0441\u0443\u0440\u0441, \u0438\u0437\u043c\u0435\u043d\u0438\u0432 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435:<\/p>\n<pre><code class=\"php\">&lt;?php  use App\\Http\\Resources\\Resource;  class UserResource extends Resource {     protected static ?string $dataKey = 'users';        public function toArray(Request $request): array     {         return [             'id' => $this->id,              'firstName' => $this->first_name,             'lastName' => $this->last_name,         ];     } }<\/code><\/pre>\n<p>\u0412\u043e\u0442 \u0438 \u0432\u0441\u0451. \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <code>UserResource::make($user)<\/code> \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c &#171;\u043f\u0440\u043e\u0441\u0442\u043e\u0439&#187; \u043e\u0431\u044a\u0435\u043a\u0442 \u0431\u0435\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u0438, \u0430 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <code>UserResource::collection($users)<\/code> &#8212; \u043a\u043b\u044e\u0447 &#171;users&#187; \u0432\u043d\u0443\u0442\u0440\u0438 &#171;data&#187;, \u043a\u0430\u043a \u0438 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u043e\u0441\u044c \u0432\u044b\u0448\u0435.<\/p>\n<h3>\u041a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442?<\/h3>\n<p>\u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u0430\u044f \u043c\u0435\u0445\u0430\u043d\u0438\u043a\u0430 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e. \u041f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f &#171;\u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e&#187; \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0445\u043e\u0442\u044c \u0447\u0435\u0440\u0435\u0437 <code>new UserResource($user)<\/code>, \u0445\u043e\u0442\u044c \u0447\u0435\u0440\u0435\u0437 <code>UserResource::make($user)<\/code>, Laravel \u0441\u043e\u0437\u0434\u0430\u0441\u0442 \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432 \u0438 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u0435\u0439. \u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0447\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0435\u0442 &#8212; \u044d\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442 <code>'status' => 'OK'<\/code> \u0432 \u043e\u0442\u0432\u0435\u0442, \u0430 \u0432\u043e\u0442 \u043f\u0440\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0435 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0441\u044f \u043d\u0430\u0448 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0438, \u0435\u0441\u043b\u0438 \u0432 \u0440\u0435\u0441\u0443\u0440\u0441\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 <code>$dataKey<\/code>, \u0442\u043e \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 JSON \u043e\u0431\u044a\u0435\u043a\u0442 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0438\u043d\u0430\u0447\u0435 \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u043c\u0435\u0442\u043e\u0434 <code>resolveDataKey<\/code> \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u044c\u043c\u0451\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u0443\u044e \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u043a\u043b\u0430\u0441\u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u0440\u0435\u0441\u0443\u0440\u0441\u0430, \u0434\u0430\u043b\u0435\u0435 \u043e\u0442\u0441\u0435\u0438\u0442 \u0432\u0441\u0451 \u0447\u0442\u043e \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u0441\u043b\u0435\u0448\u0435\u043c, \u0434\u0430\u043b\u0435\u0435 \u0432\u043e\u0437\u044c\u043c\u0451\u0442 \u0432\u0441\u0451 \u0447\u0442\u043e \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434 \u0441\u043b\u043e\u0432\u043e\u043c <code>Resource<\/code>, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442 \u0432 <code>snake_case<\/code> \u0438 \u0441\u0434\u0435\u043b\u0430\u0435\u0442 \u0432\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u043c \u0447\u0438\u0441\u043b\u0435.<\/p>\n<p>\u0417\u0432\u0443\u0447\u0438\u0442 \u0441\u043b\u043e\u0436\u043d\u043e, \u0430 \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0432\u0441\u0451 \u043f\u0440\u043e\u0449\u0435 \u043d\u0435\u043a\u0443\u0434\u0430. \u0422\u0430\u043a \u043a\u0430\u043a \u043c\u044b \u044f\u0432\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u043b\u0438 \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 \u044e\u0437\u0435\u0440\u043e\u0432 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440, \u0442\u043e \u0434\u0430\u043d\u043d\u0430\u044f \u043c\u0435\u0445\u0430\u043d\u0438\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u043d\u043e\u0432\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0438, \u0447\u0442\u043e\u0431\u044b \u0434\u043e\u043b\u0433\u043e \u043d\u0435 \u0434\u0443\u043c\u0430\u0442\u044c \u043d\u0430\u0434 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c, \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0431\u0430\u043d\u0430\u043b\u044c\u043d\u043e:<\/p>\n<pre><code class=\"php\">&lt;?php  namespace App\\Http\\Resources;  class MyWorkResource extends Resource {        public function toArray(Request $request): array     {         return [             'id' => $this->id,             'title' => $this->title,         ];     } }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0438\u043c\u0435\u043d\u0438 \u043a\u043b\u044e\u0447\u0430 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u044b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0448\u0430\u0433\u0438:<\/p>\n<pre><code>App\\Http\\Resources\\MyWorkResource MyWorkResource MyWork my_work my_works<\/code><\/pre>\n<p>\u041d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0438\u043c\u044f \u043a\u043b\u044e\u0447\u0430 &#8212; <code>my_works<\/code>:<\/p>\n<pre><code class=\"json\">{   \"data\": {     \"my_works\": []   },   \"status\": \"OK\" }<\/code><\/pre>\n<p>\u0412\u0441\u0451 \u043f\u0440\u043e\u0449\u0435 \u043d\u0435\u043a\u0443\u0434\u0430. \u0418 \u043a\u0440\u0430\u0441\u0438\u0432\u043e! ?<\/p>\n<h2>\u0411\u043e\u043d\u0443\u0441<\/h2>\n<p>\u0414\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0447\u0430\u0441\u0442\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u043c\u0435\u043d\u044f\u044e\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0445 JSON \u043a\u043b\u044e\u0447\u0435\u0439 \u0441\u043e <code>snake_case<\/code> \u043d\u0430 <code>camelCase<\/code> \u0438, \u0437\u0430\u0447\u0430\u0441\u0442\u0443\u044e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0438. \u041e\u0434\u0438\u043d \u0438\u0437 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044f \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u043b, \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b \u0436\u0443\u0442\u043a\u043e:<\/p>\n<pre><code class=\"php\">&lt;?php  class PaginatorCamelCaseResource extends PaginatorResource {     public function toArray($request): array     {         return collect(parent::toArray($request))             ->put('total', $this->total())             ->mapWithKeys(                 fn (mixed $item, string $key) => [Str::camel($key) => $item]             )->toArray();     } }<\/code><\/pre>\n<p>\u041d\u043e \u043a\u0430\u043a \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u043e-\u0447\u0435\u043b\u043e\u0432\u0435\u0447\u0435\u0441\u043a\u0438? &#8212; \u0441\u043f\u0440\u043e\u0441\u0438\u0442\u0435 \u0432\u044b. \u041b\u0435\u0433\u043a\u043e! \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u0441\u044f \u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u043d\u0430\u043c\u0438 \u043a\u043b\u0430\u0441\u0441 <code>Collection<\/code> \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u043d\u0435\u0433\u043e \u043d\u0435\u0445\u0438\u0442\u0440\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 <code>paginationInformation<\/code> \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u044b\u043c:<\/p>\n<pre><code class=\"php\">&lt;?php  public function paginationInformation(Request $request, array $paginated, array $default): array {     return [         'pagination' => [             'total' => $default['meta']['total'] ?? 0,             'perPage' => $default['meta']['per_page'] ?? 0,             'currentPage' => $default['meta']['current_page'] ?? 0,             'lastPage' => $default['meta']['last_page'] ?? 0,         ],     ]; }<\/code><\/pre>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0438\u0437 \u0432\u0441\u0435\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0430\u0433\u0438\u043d\u0430\u0446\u0438\u0438 \u043d\u0443\u0436\u043d\u044b \u043b\u0438\u0448\u044c \u043d\u043e\u043c\u0435\u0440\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446 \u0438 \u043e\u0431\u0449\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u043d\u043e \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043b\u0435\u0433\u043a\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a. \u041d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0442\u0430\u043a\u043e\u0439 \u043f\u0440\u0438\u0451\u043c \u0432\u0435\u0440\u043d\u0451\u0442 \u043e\u0431\u044a\u0435\u043a\u0442:<\/p>\n<pre><code class=\"php\">&lt;?php  $users = User::paginate();  return UserResource::collection($users);<\/code><\/pre>\n<pre><code class=\"json\">{   \"data\": {     \"users\": [       {         \"id\": 1,         \"firstName\": \"\u041d\u0438\u043d\u0430\",        \"lastName\": \"\u0414\u0440\u043e\u0437\u0434\u043e\u0432\u0430\"       },       ...     ]   },   \"pagination\": {     \"total\": 27,     \"perPage\": 5,     \"currentPage\": 1,     \"lastPage\": 6   }   \"status\": \"OK\" }<\/code><\/pre>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u0440\u0430\u0432\u043d\u0438\u043c \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u0431\u044b\u043b\u043e:<\/p>\n<pre><code class=\"json\">{     \"data\": {         \"users\": [             {                 \"id\": 1,                 \"firstName\": \"\u041d\u0438\u043d\u0430\",                 \"lastName\": \"\u0414\u0440\u043e\u0437\u0434\u043e\u0432\u0430\"             },             ...         ]     },     \"status\": \"OK\",     \"links\": {         \"first\": \"http:\\\/\\\/127.0.0.1:8000\\\/api?page=1\",         \"last\": \"http:\\\/\\\/127.0.0.1:8000\\\/api?page=6\",         \"prev\": null,         \"next\": \"http:\\\/\\\/127.0.0.1:8000\\\/api?page=2\"     },     \"meta\": {         \"current_page\": 1,         \"from\": 1,         \"last_page\": 6,         \"links\": [             {                 \"url\": null,                 \"label\": \"&amp;laquo; Previous\",                 \"active\": false             },             {                 \"url\": \"http:\\\/\\\/127.0.0.1:8000\\\/api?page=1\",                 \"label\": \"1\",                 \"active\": true             },             {                 \"url\": \"http:\\\/\\\/127.0.0.1:8000\\\/api?page=2\",                 \"label\": \"2\",                 \"active\": false             },             {                 \"url\": \"http:\\\/\\\/127.0.0.1:8000\\\/api?page=3\",                 \"label\": \"3\",                 \"active\": false             },             {                 \"url\":<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-368458","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/368458","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=368458"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/368458\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=368458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=368458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=368458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}