php - Laravel 5 - Customize JSON response on a Rest API server -
i new on laravel, migrating application slim framework to, indeed, laravel 5. googling haven't find information how customize json response. let's have:
model
<?php namespace app; use illuminate\database\eloquent\model; class user extends model { protected $visible = [ 'username', 'posts', ]; }
controller
<?php namespace app\http\controllers; use [...] /* * * implicit controller */ class usercontroller extends controller { public function getindex() { return response()->json(user::all(), 200); } }
route
route::controller('users', 'usercontroller');
what if want output data in json object like:
{"success": bool, "message": string, "data": array} // in case 'array' user::all()
?
does know whether there's library handle kind of stuff? or has addressed in laravel somehow?
n.b. know can write middleware "modify" response, not sure right solution, , painful check middleware whether response should contain error or not.
thank you.
did try
return response()->json([ 'success'=>true, 'message'=>'string', 'data'=>user::all() ]);
?
Comments
Post a Comment