inna / think-api-resource
API Resources Converter for ThinkPHP.
2.0.0
2021-09-15 08:18 UTC
Requires
- php: ^7.0 || ^8.0
- topthink/framework: ^6.0
README
API Resources Converter for ThinkPHP 6.
Installation
$ composer require inna/think-api-resource:^2.0
Usage
<?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', ]); } }