Beispiel #1
0
  */
 $rest->get('/students/{id}', function ($id) use($app, $modelToArray) {
     $model = Student::findOrFail($id);
     return $app->json($modelToArray($model, ['level']));
 })->assert('id', '\\d+');
 /**
  * Search students
  */
 $rest->get('/students/search', function () use($app) {
     $q = addslashes($app['request']->get('query'));
     $search = strtolower($q) . '%';
     $result = DB::select("SELECT `id`\n          FROM `students`\n          WHERE `name` LIKE '{$search}';\n        ");
     $result = array_map(function ($itm) {
         return $itm['id'];
     }, $result);
     $students = Student::whereIn('id', $result)->get();
     return $app->json($students);
 });
 /**
  * Student creation
  */
 $rest->post('/students', function (Request $req) use($app) {
     $data = $req->request->all();
     $s = new Student($data);
     try {
         $s->validate();
         $s->save();
         return $app->json($s, 201);
     } catch (\Models\ValidationException $e) {
         return $app->json(['errors' => $e->getErrors()], 400);
     }