flugg / laravel-responder

A Laravel Fractal package for building API responses, giving you the power of Fractal with Laravel's elegancy.
MIT License
861 stars 86 forks source link

Include message in success response #180

Open LubySagia opened 2 years ago

LubySagia commented 2 years ago

{ "status": 200, "success": true, "message": "Logged out successfully!", "data": null }

Is there any way to include a message to success respond like the above?

jupevi commented 2 years ago

You can add additional fields to the root response object by using the meta() magic method of SuccessResponseBuilder, e.g. in a controller method (the controller must use MakesResponses):

return $this->success()
             ->meta(['message' => 'Hello World!'])
             ->respond();

I don't think there's a way to add anything to the root object via a transformer class.

See: https://github.com/flugg/laravel-responder#adding-meta-data