PHP code example of codemonster-ru / api-resource

1. Go to this page and download the library: Download codemonster-ru/api-resource library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codemonster-ru / api-resource example snippets


use Codemonster\ApiResource\JsonResource;

final class UserResource extends JsonResource
{
    public function toArray(): array
    {
        return [
            'id' => $this->resource->getKey(),
            'name' => $this->resource->name,
        ];
    }
}

return (new UserResource($user))->response();

return UserResource::collection(User::all())->response();

return UserResource::paginated(
    User::query()->simplePaginate(20, $page),
    '/api/users',
    ['filter' => 'active'],
)->response();