/** * Get sepecified resource or fail with HTTP 404 * * @param int $id * @return \Illuminate\Database\Eloquent\Model */ public static function getCourseWithOrFail($id) { // in case it's not found Session::flash('http_status', 'Course not found.'); // get course with its term and its files sorted newest to oldest $course = \ATC\Course::with(['term', 'files' => function ($query) { $query->orderBy('updated_at', 'ASC'); }])->findOrFail($id); // course found Session::remove('http_status'); return $course; }
/** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { $title = 'Show Student'; // in case it's not found Session::flash('http_status', 'Student not found.'); // get a student $student = \ATC\Student::findOrFail($id); // student found Session::remove('http_status'); $courses = \ATC\Course::with('term')->where('student_id', $id)->orderBy('name', 'ASC')->get(); return view('student.show')->withTitle($title)->withStudent($student)->withCourses($courses); }
/** * Run the database seeds. * * @return void */ public function run() { // get ids of all the courses $course_ids = \ATC\Course::lists('id'); // get ids of all the files $file_ids = \ATC\File::lists('id'); // create Faker object $faker = Faker::create(); foreach ($course_ids as $course_id) { // vary the number of files per course $filesPerCourse = $faker->numberBetween(1, 50); // create array for each course to choose file_ids from $course_file_ids = $file_ids->all(); // randomize so files will be unique for each course not for all courses shuffle($course_file_ids); for ($i = 0; $i < $filesPerCourse; $i++) { DB::table('course_file')->insert(['created_at' => Carbon\Carbon::now()->toDateTimeString(), 'updated_at' => Carbon\Carbon::now()->toDateTimeString(), 'course_id' => $course_id, 'file_id' => array_pop($course_file_ids)]); } } }
/** * Remove the specified resource from storage. * * @param int $studentId * @param int $id * @return \Illuminate\Http\Response */ public function destroy($studentId, $id) { // check the student \ATC\Student::getStudentOrFail($studentId); $course = \ATC\Course::getCourseWithOrFail($id); // delete course, will cascade to delete relations to files $course->delete(); Session::flash('flash_message', $course->name . ' deleted'); // go to list view return redirect()->action('StudentController@show', [$studentId]); }
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function updateCourse(Request $request, $id) { // check the course $course = \ATC\Course::getCourseWithOrFail($request->course); // get a file $file = \ATC\File::getFileOrFail($id); // save update if ($file->saveFileCourse($course->id)) { return redirect()->action('CourseController@show', [$course->student->id, $course->id]); } else { return back()->withInput(); } }