Laravel Macroable Response

Submitted by haruncpi - 4 years ago

Laravel macro is a cool feature for extending the laravel core classes. http://bit.ly/laravel-macro

//add boot method in app/Providers/AppServiceProvider.php
public function boot()
{ 
    Response::macro('success', function ($message) {
        return ['success'=>true,'message'=>$message];
    });

    Response::macro('fail', function ($message) {
        return ['success'=>false,'message'=>$message];
    });
}

//use
return response()->success('successfully send!');

//output
{
    success: true,
    message: "Successfully send!"
}

// more detail: http://bit.ly/laravel-macro