inna / think-api-resource
ThinkPHP 的 API 资源转换器。
2.0.0
2021-09-15 08:18 UTC
Requires
- php: ^7.0 || ^8.0
- topthink/framework: ^6.0
README
ThinkPHP 6 的 API 资源转换器。
安装
$ composer require inna/think-api-resource:^2.0
使用方法
<?php use Inna\ApiResource\JsonResource; class UserResource extends JsonResource { public function toArray($request) { return [ 'id' => $this->id, 'name' => $this->name, 'email' => $this->email, 'posts' => PostResource::collection($this->whenLoad('posts')), ]; } }
<?php use Inna\ApiResource\JsonResource; class PostResource extends JsonResource { public function toArray($request) { return [ 'id' => $this->id, 'title' => $this->title, 'content' => $this->content, ]; } }
<?php class UserController { public function index() { $users = User::with('posts')->paginate(); return UserResource::collection($users); } public function show() { $user = User::find(1); return UserResource::make($user)->wrap('user')->additional([ 'foo' => 'bar', ]); } }