[Laravel] queries to JSON

1. update app/routes.php

Route::group(['prefix' => 'api/v1'], function()
{
  Route::resource("lessons", "LessonsController");
});

2. create LessonsController

$ php artisan controller:make LessonsController

3. check: php artisan routes

$ php artisan routes

Screen Shot 2014-05-16 at 12.20.58 AM

4. update app/controllers/LessonsController.php

public function index()
{
  // 1. All is bad
  // 2. No way to attach meta data
  // 3. Linking db structure to the API output
  // 4. No way to signal headers/response codes
  return Lesson::all();
}

5. browse http://localhost:8000/api/v1/lessons: get json data.(this is bad practice only for test)

Screen Shot 2014-05-16 at 12.28.54 AM

6. install chrome extension: postman

Screen Shot 2014-05-16 at 12.35.43 AM

7. hide some data if you do not show it: use $hidden

<?php
class Lesson extends \Eloquent{
  protected $fillable = ['title', 'body'];
  
  protected $hidden = ['title'];
}