krasnikov / eloquent-json
本包最新版本(1.0.1)没有提供许可证信息。
1.0.1
2019-08-17 16:30 UTC
Requires
- php: ^7.1.3
- ext-json: *
- spatie/laravel-translatable: ^4.1
This package is auto-updated.
Last update: 2024-09-21 01:47:00 UTC
README
简介
JSON:API 是一个规范,用于说明客户端如何请求获取或修改资源,以及服务器如何响应这些请求。
JSON:API 设计用于最小化客户端和服务器之间请求的数量和数据传输量。这种效率不会牺牲可读性、灵活性和可发现性。
JSON:API 要求使用 JSON:API 媒体类型(application/vnd.api+json)来交换数据。
安装
composer require krasnikov/eloquent-json
- 将
\Krasnikov\EloquentJSON\EloquentJsonServiceProvider::class,
添加到 Application 服务提供者 php artisan vendor:publish --provider="Krasnikov\EloquentJSON\EloquentJsonServiceProvider" --tag=config
php artisan vendor:publish --provider="Krasnikov\EloquentJSON\EloquentJsonServiceProvider" --tag=translations
- 在
config/jsonSpec.php
中更改日期格式和路由前缀 - 如果需要在属性中设置对象 ID,请在
config/jsonSpec.php
中设置'show_id' => true
- 在您的模型中使用
ModelJson
特性
<?php namespace App\Domain\User; use Krasnikov\EloquentJSON\Traits\ModelJson; use Illuminate\Database\Eloquent\Model; class User extends Model { use ModelJson; }
或扩展 JsonModel
类
<?php namespace App\Domain\User; use Krasnikov\EloquentJSON\JsonModel; class User extends JsonModel { }
- 在您的模型中使用参数
$allowedReferences
来定义允许的引用列表
<?php namespace App\Domain\User; use Krasnikov\EloquentJSON\Traits\ModelJson; use Illuminate\Database\Eloquent\Model; class User extends Model { use ModelJson; protected $allowedReferences = [ 'account', 'roles', 'roles.permission' ]; }
- 在您的控制器中使用
Krasnikov\EloquentJSON\Pagination::fromRequest($request)
来设置分页并发送对象到仓库 - 在您的控制器中使用
Krasnikov\EloquentJSON\Sorting::fromRequest($request)
来设置排序并发送对象到仓库。在仓库中使用方法setBuilder
来设置排序到查询构建器$sorting->setBuilder($builder)
; - 在您的控制器中使用 Model 方法
toJsonSpec()
来获取响应结构return \response()->json($users->toJsonSpec(), Response::HTTP_OK);
加载关系
GET /articles/1?include=author,comments.author HTTP/1.1
稀疏字段集
GET /articles?include=author&fields[articles]=title,body&fields[people]=name HTTP/1.1
分页
GET /articles?page[number]=2&page[size]=10 HTTP/1.1
响应示例
{ "data": [ { "type": "user", "id": 1, "attributes": { "id": 1, "name": "Admin", "email": "admin@admin.com", "emailVerifiedAt": "2019-07-30 10:46:20" }, "relationships": { "roles": { "data": [ { "id": 1, "type": "role", "relations": { "permissions": { "data": [ { "id": 1, "type": "permission" }, { "id": 2, "type": "permission" }, { "id": 3, "type": "permission" }, { "id": 4, "type": "permission" }, { "id": 5, "type": "permission" } ] } } } ] }, "account": { "data": { "id": 1, "type": "account" } }, "permissions": { "data": [] } } }, { "type": "user", "id": 2, "attributes": { "id": 2, "name": "Admin2", "email": "admin2@admin.com", "emailVerifiedAt": "2019-07-30 10:46:20" }, "relationships": { "roles": { "data": [ { "id": 2, "type": "role", "relations": { "permissions": { "data": [ { "id": 1, "type": "permission" } ] } } } ] }, "account": { "data": { "id": 2, "type": "account" } }, "permissions": { "data": [] } } } ], "meta": { "total": 2, "perPage": 20 }, "links": { "self": "http://0.0.0.0:81/api/v1.0/users?page%5Bnumber%5D=1&page%5Bsize%5D=20", "first": "http://0.0.0.0:81/api/v1.0/users?page%5Bnumber%5D=1&page%5Bsize%5D=20", "last": "http://0.0.0.0:81/api/v1.0/users?page%5Bnumber%5D=1&page%5Bsize%5D=20" }, "included": [ { "id": 1, "type": "permission", "attributes": { "id": 1, "name": "List user", "guardName": "api", "createdAt": "2019-07-30 10:46:20", "updatedAt": "2019-07-30 10:46:20" } }, { "id": 2, "type": "permission", "attributes": { "id": 2, "name": "Create user", "guardName": "api", "createdAt": "2019-07-30 10:46:20", "updatedAt": "2019-07-30 10:46:20" } }, { "id": 3, "type": "permission", "attributes": { "id": 3, "name": "Update user", "guardName": "api", "createdAt": "2019-07-30 10:46:20", "updatedAt": "2019-07-30 10:46:20" } }, { "id": 4, "type": "permission", "attributes": { "id": 4, "name": "Delete user", "guardName": "api", "createdAt": "2019-07-30 10:46:20", "updatedAt": "2019-07-30 10:46:20" } }, { "id": 5, "type": "permission", "attributes": { "id": 5, "name": "Get user", "guardName": "api", "createdAt": "2019-07-30 10:46:21", "updatedAt": "2019-07-30 10:46:21" } }, { "id": 1, "type": "role", "attributes": { "id": 1, "name": "Admin", "guardName": "api", "createdAt": "2019-07-30 10:46:21", "updatedAt": "2019-07-30 10:46:21" } }, { "id": 1, "type": "account", "attributes": { "id": 1, "userId": 1, "firstName": "Alex", "lastName": "Krasnikov", "phone": "88888888888" } }, { "id": 2, "type": "role", "attributes": { "id": 2, "name": "User", "guardName": "api", "createdAt": "2019-07-30 10:46:21", "updatedAt": "2019-07-30 10:46:21" } }, { "id": 2, "type": "account", "attributes": { "id": 2, "userId": 2, "firstName": "Alex", "lastName": "Krasnikov", "phone": "88888888888" } } ] }