Skip to content

Instantly share code, notes, and snippets.

@InnaTarasyan
Created November 29, 2025 16:33
Show Gist options
  • Select an option

  • Save InnaTarasyan/4dc2f276e7fd92a900a4d019011f1227 to your computer and use it in GitHub Desktop.

Select an option

Save InnaTarasyan/4dc2f276e7fd92a900a4d019011f1227 to your computer and use it in GitHub Desktop.
Laravel Response::success() / Response::error() Macro
// app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Response;
public function boot()
{
Response::macro('success', function ($data = [], $message = 'OK', $status = 200) {
return response()->json([
'status' => 'success',
'message' => $message,
'data' => $data
], $status);
});
Response::macro('error', function ($message = 'Error', $status = 400, $errors = []) {
return response()->json([
'status' => 'error',
'message' => $message,
'errors' => $errors
], $status);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment