Skip to content

Eloquent: API 资源

介绍

在构建 API 时,您可能需要一个转换层,该层位于您的 Eloquent 模型和实际返回给应用程序用户的 JSON 响应之间。例如,您可能希望为某些用户显示特定属性,而不为其他用户显示,或者您可能希望始终在模型的 JSON 表示中包含某些关系。Eloquent 的资源类允许您以表达和简单的方式将模型和模型集合转换为 JSON。

当然,您始终可以使用它们的 toJson 方法将 Eloquent 模型或集合转换为 JSON;然而,Eloquent 资源提供了对模型及其关系的 JSON 序列化的更细粒度和强大的控制。

生成资源

要生成资源类,您可以使用 make:resource Artisan 命令。默认情况下,资源将放置在应用程序的 app/Http/Resources 目录中。资源扩展 Illuminate\Http\Resources\Json\JsonResource 类:

shell
php artisan make:resource UserResource

资源集合

除了生成转换单个模型的资源外,您还可以生成负责转换模型集合的资源。这允许您的 JSON 响应包含与给定资源的整个集合相关的链接和其他元信息。

要创建资源集合,您应该在创建资源时使用 --collection 标志。或者,在资源名称中包含单词 Collection 将指示 Laravel 应该创建一个集合资源。集合资源扩展 Illuminate\Http\Resources\Json\ResourceCollection 类:

shell
php artisan make:resource User --collection

php artisan make:resource UserCollection

概念概述

lightbulb

这是对资源和资源集合的高级概述。强烈建议您阅读本文档的其他部分,以深入了解资源提供的自定义和强大功能。

在深入了解编写资源时可用的所有选项之前,让我们首先高层次地看一下 Laravel 中资源的使用方式。资源类表示需要转换为 JSON 结构的单个模型。例如,这里是一个简单的 UserResource 资源类:

php
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    /**
     * 将资源转换为数组。
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}

每个资源类都定义了一个 toArray 方法,该方法返回应在从路由或控制器方法返回资源时转换为 JSON 的属性数组。

请注意,我们可以直接从 $this 变量访问模型属性。这是因为资源类会自动将属性和方法访问代理到底层模型,以便方便访问。一旦定义了资源,它可以从路由或控制器返回。资源通过其构造函数接受底层模型实例:

php
use App\Http\Resources\UserResource;
use App\Models\User;

Route::get('/user/{id}', function (string $id) {
    return new UserResource(User::findOrFail($id));
});

资源集合

如果您返回的是资源集合或分页响应,则应在路由或控制器中创建资源实例时使用资源类提供的 collection 方法:

php
use App\Http\Resources\UserResource;
use App\Models\User;

Route::get('/users', function () {
    return UserResource::collection(User::all());
});

请注意,这不允许添加可能需要与您的集合一起返回的自定义元数据。如果您希望自定义资源集合响应,您可以创建一个专门表示集合的资源:

shell
php artisan make:resource UserCollection

生成资源集合类后,您可以轻松定义应包含在响应中的任何元数据:

php
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;

class UserCollection extends ResourceCollection
{
    /**
     * 将资源集合转换为数组。
     *
     * @return array<int|string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'data' => $this->collection,
            'links' => [
                'self' => 'link-value',
            ],
        ];
    }
}

定义资源集合后,可以从路由或控制器返回它:

php
use App\Http\Resources\UserCollection;
use App\Models\User;

Route::get('/users', function () {
    return new UserCollection(User::all());
});

保留集合键

从路由返回资源集合时,Laravel 会重置集合的键,以便它们按数字顺序排列。但是,您可以向资源类添加一个 preserveKeys 属性,指示是否应保留集合的原始键:

php
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    /**
     * 指示资源的集合键是否应被保留。
     *
     * @var bool
     */
    public $preserveKeys = true;
}

preserveKeys 属性设置为 true 时,集合键将在从路由或控制器返回集合时被保留:

php
use App\Http\Resources\UserResource;
use App\Models\User;

Route::get('/users', function () {
    return UserResource::collection(User::all()->keyBy->id);
});

自定义底层资源类

通常,资源集合的 $this->collection 属性会自动填充为将集合中的每个项目映射到其单一资源类的结果。单一资源类被假定为集合的类名,去掉尾部的 Collection 部分。此外,根据您的个人偏好,单一资源类可能会或可能不会以 Resource 结尾。

例如,UserCollection 将尝试将给定的用户实例映射到 UserResource 资源。要自定义此行为,您可以重写资源集合的 $collects 属性:

php
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class UserCollection extends ResourceCollection
{
    /**
     * 此资源收集的资源。
     *
     * @var string
     */
    public $collects = Member::class;
}

编写资源

lightbulb

如果您尚未阅读 概念概述,强烈建议您在继续阅读本文件之前先阅读。

资源只需将给定模型转换为数组。因此,每个资源都包含一个 toArray 方法,该方法将模型的属性转换为可以从应用程序的路由或控制器返回的 API 友好数组:

php
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    /**
     * 将资源转换为数组。
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}

定义资源后,可以直接从路由或控制器返回它:

php
use App\Http\Resources\UserResource;
use App\Models\User;

Route::get('/user/{id}', function (string $id) {
    return new UserResource(User::findOrFail($id));
});

关系

如果您希望在响应中包含相关资源,可以将它们添加到资源的 toArray 方法返回的数组中。在此示例中,我们将使用 PostResource 资源的 collection 方法将用户的博客文章添加到资源响应中:

php
use App\Http\Resources\PostResource;
use Illuminate\Http\Request;

/**
 * 将资源转换为数组。
 *
 * @return array<string, mixed>
 */
public function toArray(Request $request): array
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'email' => $this->email,
        'posts' => PostResource::collection($this->posts),
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
    ];
}
lightbulb

如果您只希望在关系已经加载时才包含关系,请查看有关 条件关系 的文档。

资源集合

虽然资源将单个模型转换为数组,但资源集合将模型集合转换为数组。然而,并不绝对必要为每个模型定义资源集合类,因为所有资源都提供了一个 collection 方法,可以动态生成“临时”资源集合:

php
use App\Http\Resources\UserResource;
use App\Models\User;

Route::get('/users', function () {
    return UserResource::collection(User::all());
});

但是,如果您需要自定义返回的集合的元数据,则必须定义自己的资源集合:

php
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;

class UserCollection extends ResourceCollection
{
    /**
     * 将资源集合转换为数组。
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'data' => $this->collection,
            'links' => [
                'self' => 'link-value',
            ],
        ];
    }
}

与单一资源一样,资源集合也可以直接从路由或控制器返回:

php
use App\Http\Resources\UserCollection;
use App\Models\User;

Route::get('/users', function () {
    return new UserCollection(User::all());
});

数据包装

默认情况下,您的最外层资源在转换为 JSON 时会被包装在一个 data 键中。因此,例如,典型的资源集合响应看起来如下:

json
{
    "data": [
        {
            "id": 1,
            "name": "Eladio Schroeder Sr.",
            "email": "therese28@example.com"
        },
        {
            "id": 2,
            "name": "Liliana Mayert",
            "email": "evandervort@example.com"
        }
    ]
}

如果您希望禁用最外层资源的包装,您应该在基础的 Illuminate\Http\Resources\Json\JsonResource 类上调用 withoutWrapping 方法。通常,您应该从 AppServiceProvider 或其他在每个请求中加载的 服务提供者 中调用此方法:

php
<?php

namespace App\Providers;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * 注册任何应用程序服务。
     */
    public function register(): void
    {
        // ...
    }

    /**
     * 启动任何应用程序服务。
     */
    public function boot(): void
    {
        JsonResource::withoutWrapping();
    }
}
exclamation

withoutWrapping 方法仅影响最外层响应,并不会删除您手动添加到自己资源集合中的 data 键。

包装嵌套资源

您可以完全自由地确定资源的关系如何包装。如果您希望所有资源集合都被包装在一个 data 键中,无论它们的嵌套层级如何,您应该为每个资源定义一个资源集合类,并在 data 键中返回集合。

您可能会想知道这是否会导致您的最外层资源被包装在两个 data 键中。别担心,Laravel 永远不会让您的资源意外地双重包装,因此您不必担心您正在转换的资源集合的嵌套级别:

php
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class CommentsCollection extends ResourceCollection
{
    /**
     * 将资源集合转换为数组。
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return ['data' => $this->collection];
    }
}

数据包装和分页

通过资源响应返回分页集合时,即使已调用 withoutWrapping 方法,Laravel 也会将您的资源数据包装在一个 data 键中。这是因为分页响应始终包含有关分页器状态的 metalinks 键的信息:

json
{
    "data": [
        {
            "id": 1,
            "name": "Eladio Schroeder Sr.",
            "email": "therese28@example.com"
        },
        {
            "id": 2,
            "name": "Liliana Mayert",
            "email": "evandervort@example.com"
        }
    ],
    "links": {
        "first": "http://example.com/users?page=1",
        "last": "http://example.com/users?page=1",
        "prev": null,
        "next": null
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 1,
        "path": "http://example.com/users",
        "per_page": 15,
        "to": 10,
        "total": 10
    }
}

分页

您可以将 Laravel 分页器实例传递给资源的 collection 方法或自定义资源集合:

php
use App\Http\Resources\UserCollection;
use App\Models\User;

Route::get('/users', function () {
    return new UserCollection(User::paginate());
});

分页响应始终包含有关分页器状态的 metalinks 键的信息:

json
{
    "data": [
        {
            "id": 1,
            "name": "Eladio Schroeder Sr.",
            "email": "therese28@example.com"
        },
        {
            "id": 2,
            "name": "Liliana Mayert",
            "email": "evandervort@example.com"
        }
    ],
    "links": {
        "first": "http://example.com/users?page=1",
        "last": "http://example.com/users?page=1",
        "prev": null,
        "next": null
    },
    "meta": {
        "current_page": 1,
        "from": 1,
        "last_page": 1,
        "path": "http://example.com/users",
        "per_page": 15,
        "to": 10,
        "total": 10
    }
}

自定义分页信息

如果您希望自定义分页响应中 linksmeta 键中包含的信息,您可以在资源上定义一个 paginationInformation 方法。此方法将接收 $paginated 数据和包含 linksmeta 键的 $default 信息数组:

php
/**
 * 自定义资源的分页信息。
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  array $paginated
 * @param  array $default
 * @return array
 */
public function paginationInformation($request, $paginated, $default)
{
    $default['links']['custom'] = 'https://example.com';

    return $default;
}

条件属性

有时,您可能希望仅在满足特定条件时才在资源响应中包含某个属性。例如,您可能希望仅在当前用户是“管理员”时才包含某个值。Laravel 提供了多种辅助方法来帮助您处理这种情况。when 方法可用于有条件地将属性添加到资源响应中:

php
/**
 * 将资源转换为数组。
 *
 * @return array<string, mixed>
 */
public function toArray(Request $request): array
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'email' => $this->email,
        'secret' => $this->when($request->user()->isAdmin(), 'secret-value'),
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
    ];
}

在此示例中,secret 键仅在经过身份验证的用户的 isAdmin 方法返回 true 时才会返回到最终的资源响应中。如果该方法返回 false,则 secret 键将在发送给客户端之前从资源响应中删除。when 方法允许您以表达的方式定义资源,而无需在构建数组时使用条件语句。

when 方法还接受一个闭包作为第二个参数,仅在给定条件为 true 时计算结果值:

php
'secret' => $this->when($request->user()->isAdmin(), function () {
    return 'secret-value';
}),

whenHas 方法可用于在底层模型上实际存在某个属性时包含该属性:

php
'name' => $this->whenHas('name'),

此外,whenNotNull 方法可用于在资源响应中包含属性(如果该属性不为 null):

php
'name' => $this->whenNotNull($this->name),

合并条件属性

有时,您可能有多个属性应仅在相同条件下包含在资源响应中。在这种情况下,您可以使用 mergeWhen 方法仅在给定条件为 true 时将属性包含在响应中:

php
/**
 * 将资源转换为数组。
 *
 * @return array<string, mixed>
 */
public function toArray(Request $request): array
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'email' => $this->email,
        $this->mergeWhen($request->user()->isAdmin(), [
            'first-secret' => 'value',
            'second-secret' => 'value',
        ]),
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
    ];
}

同样,如果给定条件为 false,这些属性将在发送给客户端之前从资源响应中删除。

exclamation

mergeWhen 方法不应在混合字符串和数字键的数组中使用。此外,它不应在数字键的数组中使用,这些键不是按顺序排列的。

条件关系

除了有条件地加载属性外,您还可以根据关系是否已在模型上加载来有条件地包含资源响应中的关系。这使得控制器可以决定应在模型上加载哪些关系,而资源可以轻松地仅在实际加载时包含它们。最终,这使得在资源中避免“N+1”查询问题变得更容易。

whenLoaded 方法可用于有条件地加载关系。为了避免不必要地加载关系,此方法接受关系的名称,而不是关系本身:

php
use App\Http\Resources\PostResource;

/**
 * 将资源转换为数组。
 *
 * @return array<string, mixed>
 */
public function toArray(Request $request): array
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'email' => $this->email,
        'posts' => PostResource::collection($this->whenLoaded('posts')),
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
    ];
}

在此示例中,如果未加载关系,则 posts 键将在发送给客户端之前从资源响应中删除。

条件关系计数

除了有条件地包含关系外,您还可以根据关系的计数是否已在模型上加载来有条件地包含关系“计数”:

php
new UserResource($user->loadCount('posts'));

whenCounted 方法可用于在资源响应中有条件地包含关系的计数。如果关系的计数不存在,则此方法避免不必要地包含该属性:

php
/**
 * 将资源转换为数组。
 *
 * @return array<string, mixed>
 */
public function toArray(Request $request): array
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'email' => $this->email,
        'posts_count' => $this->whenCounted('posts'),
        'created_at' => $this->created_at,
        'updated_at' => $this->updated_at,
    ];
}

在此示例中,如果 posts 关系的计数未加载,则 posts_count 键将在发送给客户端之前从资源响应中删除。

其他类型的聚合,例如 avgsumminmax 也可以使用 whenAggregated 方法有条件地加载:

php
'words_avg' => $this->whenAggregated('posts', 'words', 'avg'),
'words_sum' => $this->whenAggregated('posts', 'words', 'sum'),
'words_min' => $this->whenAggregated('posts', 'words', 'min'),
'words_max' => $this->whenAggregated('posts', 'words', 'max'),

条件中介信息

除了有条件地在资源响应中包含关系信息外,您还可以使用 whenPivotLoaded 方法有条件地包含许多对多关系的中介表中的数据。whenPivotLoaded 方法接受中介表的名称作为第一个参数。第二个参数应该是一个闭包,返回如果模型上可用中介信息时要返回的值:

php
/**
 * 将资源转换为数组。
 *
 * @return array<string, mixed>
 */
public function toArray(Request $request): array
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'expires_at' => $this->whenPivotLoaded('role_user', function () {
            return $this->pivot->expires_at;
        }),
    ];
}

如果您的关系使用自定义中介表模型,您可以将中介表模型的实例作为第一个参数传递给 whenPivotLoaded 方法:

php
'expires_at' => $this->whenPivotLoaded(new Membership, function () {
    return $this->pivot->expires_at;
}),

如果您的中介表使用的访问器不是 pivot,您可以使用 whenPivotLoadedAs 方法:

php
/**
 * 将资源转换为数组。
 *
 * @return array<string, mixed>
 */
public function toArray(Request $request): array
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'expires_at' => $this->whenPivotLoadedAs('subscription', 'role_user', function () {
            return $this->subscription->expires_at;
        }),
    ];
}

添加元数据

某些 JSON API 标准要求在资源和资源集合响应中添加元数据。这通常包括有关资源或相关资源的 links,或有关资源本身的元数据。如果您需要返回有关资源的额外元数据,请在 toArray 方法中包含它。例如,您可能在转换资源集合时包含 links 信息:

php
/**
 * 将资源转换为数组。
 *
 * @return array<string, mixed>
 */
public function toArray(Request $request): array
{
    return [
        'data' => $this->collection,
        'links' => [
            'self' => 'link-value',
        ],
    ];
}

在从资源返回额外元数据时,您无需担心意外覆盖 Laravel 在返回分页响应时自动添加的 linksmeta 键。您定义的任何额外 links 将与分页器提供的链接合并。

顶级元数据

有时,您可能希望仅在资源是返回的最外层资源时才包含某些元数据。通常,这包括有关整个响应的元信息。要定义此元数据,请在资源类中添加一个 with 方法。此方法应返回一个数组,其中包含仅在转换的资源是最外层资源时应包含的元数据:

php
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class UserCollection extends ResourceCollection
{
    /**
     * 将资源集合转换为数组。
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return parent::toArray($request);
    }

    /**
     * 获取应与资源数组一起返回的附加数据。
     *
     * @return array<string, mixed>
     */
    public function with(Request $request): array
    {
        return [
            'meta' => [
                'key' => 'value',
            ],
        ];
    }
}

在构造资源时添加元数据

您还可以在路由或控制器中构造资源实例时添加顶级数据。所有资源都提供的 additional 方法接受一个数组,该数组应添加到资源响应中:

php
return (new UserCollection(User::all()->load('roles')))
                ->additional(['meta' => [
                    'key' => 'value',
                ]]);

资源响应

正如您已经阅读的那样,资源可以直接从路由和控制器返回:

php
use App\Http\Resources\UserResource;
use App\Models\User;

Route::get('/user/{id}', function (string $id) {
    return new UserResource(User::findOrFail($id));
});

然而,有时您可能需要在发送给客户端之前自定义传出的 HTTP 响应。有两种方法可以实现这一点。首先,您可以将 response 方法链接到资源上。此方法将返回一个 Illuminate\Http\JsonResponse 实例,使您可以完全控制响应的头部:

php
use App\Http\Resources\UserResource;
use App\Models\User;

Route::get('/user', function () {
    return (new UserResource(User::find(1)))
                ->response()
                ->header('X-Value', 'True');
});

或者,您可以在资源内部定义一个 withResponse 方法。此方法将在资源作为响应中的最外层资源返回时被调用:

php
<?php

namespace App\Http\Resources;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    /**
     * 将资源转换为数组。
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
        ];
    }

    /**
     * 自定义资源的传出响应。
     */
    public function withResponse(Request $request, JsonResponse $response): void
    {
        $response->header('X-Value', 'True');
    }
}