public function run() { $list = new TaskList(); $list->name = 'My first list'; $list->user_id = 1; $list->save(); }
| It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the Closure to execute when that URI is requested. | */ Route::group(array('prefix' => 'v1', 'before' => 'api.auth|api.limit'), function () { // Get all lists Route::get('lists', function () { $lists = Auth::user()->tasklists; return Response::json($lists->toArray()); }); // Create new list Route::post('lists', function () { $list = new TaskList(Input::get()); $list->validate(); $list->user_id = Auth::user()->id; if (!$list->save()) { App::abort(500, 'List was not saved'); } return Response::json($list->toArray(), 201); }); // Get list by ID Route::get('lists/{id}', function ($id) { $list = TaskList::findByOwnerAndId(Auth::user(), $id); return Response::json($list->toArray()); })->where('id', '\\d+'); // Update list by ID Route::put('lists/{id}', function ($id) { $list = TaskList::findByOwnerAndId(Auth::user(), $id); $list->fill(Input::get()); $list->validate(); if (!$list->save()) {
if ($_POST['action'] == "new_list") { if (isset($_POST['name'])) { if (empty(trim($_POST['name']))) { echo json_encode(array('status' => 'validation', 'reason' => 'Your task list name is empty!')); die; } /*if (strlen($_POST['name']) > 32) { echo json_encode(array('status'=>'validation', 'reason' => 'Your task list name is too long!')); die(); }*/ $tasklist = new TaskList(); $tasklist->setUserId(Authentication::getUserObject()->getId()); $tasklist->setName($_POST['name']); $tasklist->setIcon($_POST['icon']); $tasklist->save(); echo json_encode(array('status' => 'success', 'tasklist-id' => $tasklist->getId())); } else { echo json_encode(array('status' => 'error', 'reason' => 'List name not supplied.')); } } else { if ($_POST['action'] == "delete_list") { if (isset($_POST['id'])) { $list = TaskList::get($where = array('id' => $_POST['id']))[0]; foreach (Task::get($where = array('list_id' => $list->getId())) as $task) { $task->delete(); } $list->delete(); echo json_encode(array('status' => 'success')); } } else {