I am completely new on Laravel, I am migrating my application from Slim Framework to, indeed, Laravel 5. Googling I haven't find much information about how to customize a JSON response. Let's say I 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 I want to output that data in a JSON object like:
{"success": bool, "message": string, "data": array}
// in this case 'array' would be User::all()
?
Does anyone know whether there's a library to handle this kind of stuff? Or Has anyone already addressed this in laravel somehow?
N.B. I know I can write a Middleware to "modify" the response, but I am not sure it is the right solution, and it is also painful to check into the middleware whether the response should contain an error or not.
Thank you.