<?php
namespace App\Http\Controllers\Api;
use Guanguans\LaravelApiResponse\Support\Traits\ApiResponseFactory;
use Guanguans\LaravelApiResponse\Tests\Laravel\Models\User;
use Illuminate\Http\JsonResponse;
class Controller extends \App\Http\Controllers\Controller
{
use ApiResponseFactory;
public function example(): JsonResponse
{
$users = User::query()->with(['country', 'posts'])->get();
return $this->apiResponse()->success($users);
}
}
{
"status": true,
"code": 200,
"message": "OK",
"data": [
{
"id": 1,
"name": "John",
"country_id": 1,
"created_at": "2024-01-01 00:00:01",
"updated_at": "2024-01-01 00:00:01",
"country": {
"id": 1,
"name": "China",
"created_at": "2024-01-01 00:00:01",
"updated_at": "2024-01-01 00:00:01"
},
"posts": [
{
"id": 1,
"title": "PHP is the best language!",
"user_id": 1,
"created_at": "2024-01-01 00:00:01",
"updated_at": "2024-01-01 00:00:01"
},
{
"id": 2,
"title": "JAVA is the best language!",
"user_id": 1,
"created_at": "2024-01-01 00:00:02",
"updated_at": "2024-01-01 00:00:02"
},
{
"id": 3,
"title": "Python is the best language!",
"user_id": 1,
"created_at": "2024-01-01 00:00:03",
"updated_at": "2024-01-01 00:00:03"
}
]
},
{
"id": 2,
"name": "Tom",
"country_id": 2,
"created_at": "2024-01-01 00:00:02",
"updated_at": "2024-01-01 00:00:02",
"country": {
"id": 2,
"name": "USA",
"created_at": "2024-01-01 00:00:02",
"updated_at": "2024-01-01 00:00:02"
},
"posts": [
{
"id": 4,
"title": "Go is the best language!",
"user_id": 2,
"created_at": "2024-01-01 00:00:04",
"updated_at": "2024-01-01 00:00:04"
},
{
"id": 5,
"title": "JavaScript is the best language!",
"user_id": 2,
"created_at": "2024-01-01 00:00:05",
"updated_at": "2024-01-01 00:00:05"
},
{
"id": 6,
"title": "Ruby is the best language!",
"user_id": 2,
"created_at": "2024-01-01 00:00:06",
"updated_at": "2024-01-01 00:00:06"
}
]
},
{
"id": 3,
"name": "Jerry",
"country_id": 3,
"created_at": "2024-01-01 00:00:03",
"updated_at": "2024-01-01 00:00:03",
"country": {
"id": 3,
"name": "Japan",
"created_at": "2024-01-01 00:00:03",
"updated_at": "2024-01-01 00:00:03"
},
"posts": [
{
"id": 7,
"title": "C is the best language!",
"user_id": 3,
"created_at": "2024-01-01 00:00:07",
"updated_at": "2024-01-01 00:00:07"
}
]
},
{
"id": 4,
"name": "Jack",
"country_id": 4,
"created_at": "2024-01-01 00:00:04",
"updated_at": "2024-01-01 00:00:04",
"country": {
"id": 4,
"name": "Korea",
"created_at": "2024-01-01 00:00:04",
"updated_at": "2024-01-01 00:00:04"
},
"posts": []
},
{
"id": 5,
"name": "Rose",
"country_id": 5,
"created_at": "2024-01-01 00:00:05",
"updated_at": "2024-01-01 00:00:05",
"country": {
"id": 5,
"name": "UK",
"created_at": "2024-01-01 00:00:05",
"updated_at": "2024-01-01 00:00:05"
},
"posts": []
},
{
"id": 6,
"name": "Lucy",
"country_id": 6,
"created_at": "2024-01-01 00:00:06",
"updated_at": "2024-01-01 00:00:06",
"country": {
"id": 6,
"name": "France",
"created_at": "2024-01-01 00:00:06",
"updated_at": "2024-01-01 00:00:06"
},
"posts": []
},
{
"id": 7,
"name": "Lily",
"country_id": 7,
"created_at": "2024-01-01 00:00:07",
"updated_at": "2024-01-01 00:00:07",
"country": {
"id": 7,
"name": "Germany",
"created_at": "2024-01-01 00:00:07",
"updated_at": "2024-01-01 00:00:07"
},
"posts": []
}
],
"error": {}
}
laravel-api-response - 规范化和标准化 Laravel API 响应数据结构
源码
guanguans/laravel-api-response
功能
环境要求
安装
配置
发布文件(可选)
php artisan vendor:publish --provider="Guanguans\\LaravelApiResponse\\ServiceProvider" --ansi -v使用
快速开始
详情
默认响应结构
详情
{ "status": "boolean", "code": "integer", "message": "string", "data": "mixed", "error": "object" }默认响应示例
model
{ "status": true, "code": 200, "message": "OK", "data": { "id": 1, "name": "John", "country_id": 1, "created_at": "2024-01-01 00:00:01", "updated_at": "2024-01-01 00:00:01", "country": { "id": 1, "name": "China", "created_at": "2024-01-01 00:00:01", "updated_at": "2024-01-01 00:00:01" }, "posts": [ { "id": 1, "title": "PHP is the best language!", "user_id": 1, "created_at": "2024-01-01 00:00:01", "updated_at": "2024-01-01 00:00:01" }, { "id": 2, "title": "JAVA is the best language!", "user_id": 1, "created_at": "2024-01-01 00:00:02", "updated_at": "2024-01-01 00:00:02" }, { "id": 3, "title": "Python is the best language!", "user_id": 1, "created_at": "2024-01-01 00:00:03", "updated_at": "2024-01-01 00:00:03" } ] }, "error": {} }eloquent collection
{ "status": true, "code": 200, "message": "OK", "data": [ { "id": 1, "name": "John", "country_id": 1, "created_at": "2024-01-01 00:00:01", "updated_at": "2024-01-01 00:00:01", "country": { "id": 1, "name": "China", "created_at": "2024-01-01 00:00:01", "updated_at": "2024-01-01 00:00:01" }, "posts": [ { "id": 1, "title": "PHP is the best language!", "user_id": 1, "created_at": "2024-01-01 00:00:01", "updated_at": "2024-01-01 00:00:01" }, { "id": 2, "title": "JAVA is the best language!", "user_id": 1, "created_at": "2024-01-01 00:00:02", "updated_at": "2024-01-01 00:00:02" }, { "id": 3, "title": "Python is the best language!", "user_id": 1, "created_at": "2024-01-01 00:00:03", "updated_at": "2024-01-01 00:00:03" } ] }, { "id": 2, "name": "Tom", "country_id": 2, "created_at": "2024-01-01 00:00:02", "updated_at": "2024-01-01 00:00:02", "country": { "id": 2, "name": "USA", "created_at": "2024-01-01 00:00:02", "updated_at": "2024-01-01 00:00:02" }, "posts": [ { "id": 4, "title": "Go is the best language!", "user_id": 2, "created_at": "2024-01-01 00:00:04", "updated_at": "2024-01-01 00:00:04" }, { "id": 5, "title": "JavaScript is the best language!", "user_id": 2, "created_at": "2024-01-01 00:00:05", "updated_at": "2024-01-01 00:00:05" }, { "id": 6, "title": "Ruby is the best language!", "user_id": 2, "created_at": "2024-01-01 00:00:06", "updated_at": "2024-01-01 00:00:06" } ] }, { "id": 3, "name": "Jerry", "country_id": 3, "created_at": "2024-01-01 00:00:03", "updated_at": "2024-01-01 00:00:03", "country": { "id": 3, "name": "Japan", "created_at": "2024-01-01 00:00:03", "updated_at": "2024-01-01 00:00:03" }, "posts": [ { "id": 7, "title": "C is the best language!", "user_id": 3, "created_at": "2024-01-01 00:00:07", "updated_at": "2024-01-01 00:00:07" } ] }, { "id": 4, "name": "Jack", "country_id": 4, "created_at": "2024-01-01 00:00:04", "updated_at": "2024-01-01 00:00:04", "country": { "id": 4, "name": "Korea", "created_at": "2024-01-01 00:00:04", "updated_at": "2024-01-01 00:00:04" }, "posts": [] }, { "id": 5, "name": "Rose", "country_id": 5, "created_at": "2024-01-01 00:00:05", "updated_at": "2024-01-01 00:00:05", "country": { "id": 5, "name": "UK", "created_at": "2024-01-01 00:00:05", "updated_at": "2024-01-01 00:00:05" }, "posts": [] }, { "id": 6, "name": "Lucy", "country_id": 6, "created_at": "2024-01-01 00:00:06", "updated_at": "2024-01-01 00:00:06", "country": { "id": 6, "name": "France", "created_at": "2024-01-01 00:00:06", "updated_at": "2024-01-01 00:00:06" }, "posts": [] }, { "id": 7, "name": "Lily", "country_id": 7, "created_at": "2024-01-01 00:00:07", "updated_at": "2024-01-01 00:00:07", "country": { "id": 7, "name": "Germany", "created_at": "2024-01-01 00:00:07", "updated_at": "2024-01-01 00:00:07" }, "posts": [] } ], "error": {} }simple paginate
{ "status": true, "code": 200, "message": "OK", "data": { "data": [ { "id": 1, "name": "John", "country_id": 1, "created_at": "2024-01-01 00:00:01", "updated_at": "2024-01-01 00:00:01", "country": { "id": 1, "name": "China", "created_at": "2024-01-01 00:00:01", "updated_at": "2024-01-01 00:00:01" }, "posts": [ { "id": 1, "title": "PHP is the best language!", "user_id": 1, "created_at": "2024-01-01 00:00:01", "updated_at": "2024-01-01 00:00:01" }, { "id": 2, "title": "JAVA is the best language!", "user_id": 1, "created_at": "2024-01-01 00:00:02", "updated_at": "2024-01-01 00:00:02" }, { "id": 3, "title": "Python is the best language!", "user_id": 1, "created_at": "2024-01-01 00:00:03", "updated_at": "2024-01-01 00:00:03" } ] }, { "id": 2, "name": "Tom", "country_id": 2, "created_at": "2024-01-01 00:00:02", "updated_at": "2024-01-01 00:00:02", "country": { "id": 2, "name": "USA", "created_at": "2024-01-01 00:00:02", "updated_at": "2024-01-01 00:00:02" }, "posts": [ { "id": 4, "title": "Go is the best language!", "user_id": 2, "created_at": "2024-01-01 00:00:04", "updated_at": "2024-01-01 00:00:04" }, { "id": 5, "title": "JavaScript is the best language!", "user_id": 2, "created_at": "2024-01-01 00:00:05", "updated_at": "2024-01-01 00:00:05" }, { "id": 6, "title": "Ruby is the best language!", "user_id": 2, "created_at": "2024-01-01 00:00:06", "updated_at": "2024-01-01 00:00:06" } ] }, { "id": 3, "name": "Jerry", "country_id": 3, "created_at": "2024-01-01 00:00:03", "updated_at": "2024-01-01 00:00:03", "country": { "id": 3, "name": "Japan", "created_at": "2024-01-01 00:00:03", "updated_at": "2024-01-01 00:00:03" }, "posts": [ { "id": 7, "title": "C is the best language!", "user_id": 3, "created_at": "2024-01-01 00:00:07", "updated_at": "2024-01-01 00:00:07" } ] } ], "links": { "first": "http:\/\/localhost?page=1", "last": null, "prev": null, "next": "http:\/\/localhost?page=2" }, "meta": { "current_page": 1, "from": 1, "path": "http:\/\/localhost", "per_page": 3, "to": 3 } }, "error": {} }resource
{ "status": true, "code": 200, "message": "OK", "data": { "id": 1, "name": "John", "country_id": 1, "created_at": "2024-01-01 00:00:01", "updated_at": "2024-01-01 00:00:01", "country": { "id": 1, "name": "China", "created_at": "2024-01-01 00:00:01", "updated_at": "2024-01-01 00:00:01" }, "posts": [ { "id": 1, "title": "PHP is the best language!", "user_id": 1, "created_at": "2024-01-01 00:00:01", "updated_at": "2024-01-01 00:00:01" }, { "id": 2, "title": "JAVA is the best language!", "user_id": 1, "created_at": "2024-01-01 00:00:02", "updated_at": "2024-01-01 00:00:02" }, { "id": 3, "title": "Python is the best language!", "user_id": 1, "created_at": "2024-01-01 00:00:03", "updated_at": "2024-01-01 00:00:03" } ] }, "error": {} }resource collection
{ "status": true, "code": 200, "message": "OK", "data": { "data": [ { "id": 1, "name": "John", "country_id": 1, "created_at": "2024-01-01 00:00:01", "updated_at": "2024-01-01 00:00:01", "country": { "id": 1, "name": "China", "created_at": "2024-01-01 00:00:01", "updated_at": "2024-01-01 00:00:01" }, "posts": [ { "id": 1, "title": "PHP is the best language!", "user_id": 1, "created_at": "2024-01-01 00:00:01", "updated_at": "2024-01-01 00:00:01" }, { "id": 2, "title": "JAVA is the best language!", "user_id": 1, "created_at": "2024-01-01 00:00:02", "updated_at": "2024-01-01 00:00:02" }, { "id": 3, "title": "Python is the best language!", "user_id": 1, "created_at": "2024-01-01 00:00:03", "updated_at": "2024-01-01 00:00:03" } ] }, { "id": 2, "name": "Tom", "country_id": 2, "created_at": "2024-01-01 00:00:02", "updated_at": "2024-01-01 00:00:02", "country": { "id": 2, "name": "USA", "created_at": "2024-01-01 00:00:02", "updated_at": "2024-01-01 00:00:02" }, "posts": [ { "id": 4, "title": "Go is the best language!", "user_id": 2, "created_at": "2024-01-01 00:00:04", "updated_at": "2024-01-01 00:00:04" }, { "id": 5, "title": "JavaScript is the best language!", "user_id": 2, "created_at": "2024-01-01 00:00:05", "updated_at": "2024-01-01 00:00:05" }, { "id": 6, "title": "Ruby is the best language!", "user_id": 2, "created_at": "2024-01-01 00:00:06", "updated_at": "2024-01-01 00:00:06" } ] }, { "id": 3, "name": "Jerry", "country_id": 3, "created_at": "2024-01-01 00:00:03", "updated_at": "2024-01-01 00:00:03", "country": { "id": 3, "name": "Japan", "created_at": "2024-01-01 00:00:03", "updated_at": "2024-01-01 00:00:03" }, "posts": [ { "id": 7, "title": "C is the best language!", "user_id": 3, "created_at": "2024-01-01 00:00:07", "updated_at": "2024-01-01 00:00:07" } ] }, { "id": 4, "name": "Jack", "country_id": 4, "created_at": "2024-01-01 00:00:04", "updated_at": "2024-01-01 00:00:04", "country": { "id": 4, "name": "Korea", "created_at": "2024-01-01 00:00:04", "updated_at": "2024-01-01 00:00:04" }, "posts": [] }, { "id": 5, "name": "Rose", "country_id": 5, "created_at": "2024-01-01 00:00:05", "updated_at": "2024-01-01 00:00:05", "country": { "id": 5, "name": "UK", "created_at": "2024-01-01 00:00:05", "updated_at": "2024-01-01 00:00:05" }, "posts": [] }, { "id": 6, "name": "Lucy", "country_id": 6, "created_at": "2024-01-01 00:00:06", "updated_at": "2024-01-01 00:00:06", "country": { "id": 6, "name": "France", "created_at": "2024-01-01 00:00:06", "updated_at": "2024-01-01 00:00:06" }, "posts": [] }, { "id": 7, "name": "Lily", "country_id": 7, "created_at": "2024-01-01 00:00:07", "updated_at": "2024-01-01 00:00:07", "country": { "id": 7, "name": "Germany", "created_at": "2024-01-01 00:00:07", "updated_at": "2024-01-01 00:00:07" }, "posts": [] } ] }, "error": {} }error
{ "status": false, "code": 400, "message": "This is an error.", "data": {}, "error": {} }exception
{ "status": false, "code": 500, "message": "Internal Server Error", "data": {}, "error": { "message": "Server Error" } }debug exception
{ "status": false, "code": 500, "message": "This is a runtime exception.", "data": {}, "error": { "message": "This is a runtime exception.", "exception": "RuntimeException", "file": "\/tests\/Feature\/ExceptionDataTypesTest.php", "line": 45, "trace": [ { "function": "{closure}", "class": "P\\Tests\\Feature\\ExceptionDataTypesTest", "type": "->" }, { "file": "\/vendor\/pestphp\/pest\/src\/Factories\/TestCaseFactory.php", "line": 151, "function": "call_user_func" }, { "function": "Pest\\Factories\\{closure}", "class": "P\\Tests\\Feature\\ExceptionDataTypesTest", "type": "->" }, { "file": "\/vendor\/pestphp\/pest\/src\/Concerns\/Testable.php", "line": 301, "function": "call_user_func_array" }, { "file": "\/vendor\/pestphp\/pest\/src\/Support\/ExceptionTrace.php", "line": 29, "function": "Pest\\Concerns\\{closure}", "class": "P\\Tests\\Feature\\ExceptionDataTypesTest", "type": "->" }, { "file": "\/vendor\/pestphp\/pest\/src\/Concerns\/Testable.php", "line": 302, "function": "ensure", "class": "Pest\\Support\\ExceptionTrace", "type": "::" }, { "file": "\/vendor\/pestphp\/pest\/src\/Concerns\/Testable.php", "line": 278, "function": "__callClosure", "class": "P\\Tests\\Feature\\ExceptionDataTypesTest", "type": "->" }, { "file": "\/vendor\/phpunit\/phpunit\/src\/Framework\/TestCase.php", "line": 1617, "function": "__test", "class": "P\\Tests\\Feature\\ExceptionDataTypesTest", "type": "->" }, { "file": "\/vendor\/phpunit\/phpunit\/src\/Framework\/TestCase.php", "line": 1223, "function": "runTest", "class": "PHPUnit\\Framework\\TestCase", "type": "->" }, { "file": "\/vendor\/phpunit\/phpunit\/src\/Framework\/TestResult.php", "line": 729, "function": "runBare", "class": "PHPUnit\\Framework\\TestCase", "type": "->" }, { "file": "\/vendor\/phpunit\/phpunit\/src\/Framework\/TestCase.php", "line": 973, "function": "run", "class": "PHPUnit\\Framework\\TestResult", "type": "->" }, { "file": "\/vendor\/phpunit\/phpunit\/src\/Framework\/TestSuite.php", "line": 685, "function": "run", "class": "PHPUnit\\Framework\\TestCase", "type": "->" }, { "file": "\/vendor\/phpunit\/phpunit\/src\/Framework\/TestSuite.php", "line": 685, "function": "run", "class": "PHPUnit\\Framework\\TestSuite", "type": "->" }, { "file": "\/vendor\/phpunit\/phpunit\/src\/TextUI\/TestRunner.php", "line": 651, "function": "run", "class": "PHPUnit\\Framework\\TestSuite", "type": "->" }, { "file": "\/vendor\/phpunit\/phpunit\/src\/TextUI\/Command.php", "line": 146, "function": "run", "class": "PHPUnit\\TextUI\\TestRunner", "type": "->" }, { "file": "\/vendor\/pestphp\/pest\/src\/Console\/Command.php", "line": 119, "function": "run", "class": "PHPUnit\\TextUI\\Command", "type": "->" }, { "file": "\/vendor\/pestphp\/pest\/bin\/pest", "line": 61, "function": "run", "class": "Pest\\Console\\Command", "type": "->" }, { "file": "\/vendor\/pestphp\/pest\/bin\/pest", "line": 62, "function": "{closure}" }, { "file": "\/vendor\/bin\/pest", "line": 115, "function": "include" } ] } }exception handler
{ "status": false, "code": 500, "message": "Internal Server Error", "data": {}, "error": { "message": "Server Error" } }locale exception
{ "status": false, "code": 500, "message": "内部服务器错误", "data": {}, "error": { "message": "Server Error" } }更多例子...
FAQ
如何自定义管道
如何自定义异常管道
如何在单个 api 中自定义管道
如何始终以成功的 http 状态代码进行响应
api-response.pipes.StatusCodePipe如何本地化消息
composer require --dev laravel-lang/http-statuses或创建语言文件resources/lang/***/http-statuses.phphttp 状态的快捷方法
参考项目
原文连接