title: Laravel 5 - Customize JSON response on a Rest API server
body: <p>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:</p>
  
  <p><strong>MODEL</strong></p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class User extends Model
  {
      protected $visible = [
          'username', 'posts',
      ]; 
  }
  </code></pre>
  
  <p><strong>CONTROLLER</strong></p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use [...]
  
  /* *
   *  Implicit controller
   */
  class UserController extends Controller
  {
      public function getIndex()
      {
          return response()-&gt;json(User::all(), 200);
      }
  }
  </code></pre>
  
  <p><strong>ROUTE</strong></p>
  
  <pre><code>Route::controller('users', 'UserController');
  </code></pre>
  
  <p>What if I want to output that data in a JSON object like:</p>
  
  <pre><code>{"success": bool, "message": string, "data": array} 
  // in this case 'array' would be User::all()
  </code></pre>
  
  <p>?</p>
  
  <p>Does anyone know whether there's a library to handle this kind of stuff? Or Has anyone already addressed this in laravel somehow? </p>
  
  <p>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.</p>
  
  <p>Thank you.</p>