1. Go to this page and download the library: Download imran/laravel-api-toolkit 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/ */
imran / laravel-api-toolkit example snippets
return ApiResponse::success(User::paginate(15));
// or with a resource: return ApiResponse::success(UserResource::collection(User::paginate(15)));
use Imran\ApiToolkit\Traits\ApiResponser;
class UserController extends Controller
{
use ApiResponser;
public function show(User $user)
{
return $this->success($user);
}
public function store(StoreUserRequest $request)
{
$user = User::create($request->validated());
return $this->created($user);
}
}
class Controller extends BaseController
{
use ApiResponser {
success as protected apiSuccess;
error as protected apiError;
}
// your own success()/error() methods keep working; call apiSuccess()/apiError() for this package's versions
}
'exception_map' => [
\App\Exceptions\InsufficientBalanceException::class => [
'status' => 422,
'error_code' => 'INSUFFICIENT_BALANCE',
'message' => 'The account balance is too low for this operation.',
],
],
class InsufficientBalanceException extends \Exception
{
public function errors(): array
{
return ['balance' => ['The account balance is too low for this operation.']];
}
}