select('courses.id', 'courses.code', 'courses.title', 'courses.syllabus'); // Filter courses by faculty or id if ($request->session()->get('filter') === 'f') { $courses->join('departments', 'courses.dept_id', '=', 'departments.id')->where('faculty_id', '=', $request->session()->get('faculty')); } else if ($request->session()->get('filter') === 'd') { $courses->where('dept_id', '=', $request->session()->get('department')); } // dd($courses->toSql()); // dd($courses->get()); return view('courses.index', [ 'courses' => $courses->get()->sort('cmpCourseCode'), 'semesters' => Semester::orderBy('code', 'asc')->get(), ]); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // TODO: Validate syllabus is pdf $course = Course::create( $request->validate([ 'code' => ['required','unique:courses','size:8','regex:/[A-Za-z]{4}[0-9]{4}/', 'string'], 'title' => ['required','max:255', 'string'], 'dept_id' => ['required','exists:departments,id', 'integer'], ])); if ($request->file('syllabus')) { $path = $request->file('syllabus')->store('course_syllabus'); $course->update(['syllabus' => '/storage/'.$path]); } return redirect('/course/' . $course->id); } /** * Display the specified resource. * * @param \App\Course $course * @return \Illuminate\Http\Response */ public function show(Course $course) { $course->loadMissing(['semesters.sections.professors']); return view('courses.show', [ 'course' => $course, ]); } /** * Show the form for editing the specified resource. * * @param \App\Course $course * @return \Illuminate\Http\Response */ public function edit(Course $course) { // } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\Course $course * @return \Illuminate\Http\Response */ public function update(Request $request, Course $course) { $data = array_filter($request->validate([ 'code' => ['nullable', 'unique:courses','size:8','regex:/[A-Za-z]{4}[0-9]{4}/', 'string'], 'title' => ['nullable', 'max:255', 'string'], 'dept_id' => ['nullable', 'exists:departments,id', 'integer'], ])); $course->update($data); if ($request->file('syllabus')) { $path = $request->file('syllabus')->store('course_syllabus'); $course->update(['syllabus' => '/storage/'.$path]); } // dd($path); // dd(asset($path)); // dd($course->syllabus); // return redirect($course->syllabus); return redirect()->back(); } /** * Remove the specified resource from storage. * * @param \App\Course $course * @return \Illuminate\Http\Response */ public function destroy(Course $course) { // } }