/** * Generates students * @param int $amount */ protected function _generateStudents($amount) { $emailPrefix = rand(100000, 999999); for ($i = 0, $j = $amount; $i !== $j; $i++) { $name = $this->_getName() . '-' . $i; $data = ['name' => $name, 'birth_date' => date('Y-m-d', rand(0, 946684800)), 'email' => $name . $emailPrefix . '-' . $i . '@generated.com', 'level_id' => Enum::get('language_levels.*')[rand(0, 5)]->id]; $s = new Student($data); try { $s->validate(); $s->save(); } catch (\Exception $e) { // do nothing } } }
try { $s->validate(); $s->save(); return $app->json($s, 201); } catch (\Models\ValidationException $e) { return $app->json(['errors' => $e->getErrors()], 400); } }); /** * Bind teacher to student */ $rest->post('/student-teacher', function (Request $req) use($app) { $data = $req->request->all(); $st = new StudentTeacher($data); try { $st->validate(); $st->save(); return $app->json($st, 201); } catch (\Models\ValidationException $e) { return $app->json(['errors' => $e->getErrors()], 400); } }); /** * List of language levels */ $rest->get('/language-levels', function () use($app) { $levels = Enum::get('language_levels.*'); return $app->json($levels); }); $app->mount('/rest', $rest); };