PHP code example of tanmo / laravel-api

1. Go to this page and download the library: Download tanmo/laravel-api 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/ */

    

tanmo / laravel-api example snippets


php artisan vendor:publish --provider="Tanmo\Api\Providers\ApiServiceProvider"


use \Tanmo\Api\Traits\Helpers;

class UserController extends Controller
{
    use Helpers;
    
    public function show($id)
    {
        return $this->response()->item(User::find($id), UserResource::class);
    }
}

return api()->item(User::find(1), UserResource::class);

return api()->collection(User::all(), UserCollection::class); //相当于 return new UserCollection(User::all());

return api()->collection(User::all(), UserResource::class); //相当于 return UserResource::collection(User::all())

// 分页,同样支持 Collection 和 Resource
return api()->collection(User::paginate(5), UserCollection::class);

return api()->item(User::find(1), UserResource::class)->setMeta(['key' => 'value'])

return api()->created();
return api()->accepted();
return api()->noContent();

api()->errorForbidden();
api()->errorNotFound();
api()->errorBadRequest();
api()->errorInternal();
api()->errorUnauthorized();
api()->errorMethodNotAllowed();