1. Go to this page and download the library: Download rawnoq/laravel-api-response 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/ */
rawnoq / laravel-api-response example snippets
use function respond;
// Success response
return respond()->success($data, 'Operation successful');
// Error response
return respond()->error('Something went wrong', null, 400);
// Custom response
return respond()->response($data, 'Custom message', 200, true, 'custom-action');
// In your controller
public function index()
{
$users = User::all();
return respond()->success($users, 'Users retrieved successfully');
}
public function store(Request $request)
{
$user = User::create($request->validated());
return respond()->created($user, 'User created successfully');
}
public function show($id)
{
$user = User::findOrFail($id);
return respond()->ok('User retrieved successfully', $user);
}
// 200 OK
return respond()->ok('Operation successful', $data);
// 201 Created
return respond()->created($data, 'Resource created successfully');
// 202 Accepted
return respond()->accepted('Request accepted', $data);
// 204 No Content
return respond()->noContent('Resource deleted successfully');
// 400 Bad Request
return respond()->badRequest('Invalid request data');
// 401 Unauthorized
return respond()->unauthorized('Authentication alServerError('An error occurred');
// 502 Bad Gateway
return respond()->badGateway('Bad gateway');
// 503 Service Unavailable
return respond()->serviceUnavailable('Service temporarily unavailable');
// 504 Gateway Timeout
return respond()->gatewayTimeout('Gateway timeout');
public function index()
{
$users = User::paginate(15);
return respond()->success($users, 'Users retrieved successfully');
}