Ei kuvausta

CourseController.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Course;
  4. use App\Semester;
  5. use App\User;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\Redirect;
  9. use Illuminate\Support\Facades\Storage;
  10. class CourseController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. *
  15. * @return \Illuminate\Http\Response
  16. */
  17. public function index()
  18. {
  19. return view('courses.index', [
  20. 'courses' => Course::where('dept_id', '=', 1)->with('sections:id,course_id,semester_code')->get()->sort('cmpCourseCode'),
  21. 'semesters' => Semester::orderBy('code', 'asc')->get(),
  22. ]);
  23. }
  24. /**
  25. * Show the form for creating a new resource.
  26. *
  27. * @return \Illuminate\Http\Response
  28. */
  29. public function create()
  30. {
  31. }
  32. /**
  33. * Store a newly created resource in storage.
  34. *
  35. * @param \Illuminate\Http\Request $request
  36. * @return \Illuminate\Http\Response
  37. */
  38. public function store(Request $request)
  39. {
  40. // dd($request->validate(['code' => ['required','unique:courses','size:8','regex:/[A-Za-z]{4}[0-9]{4}/', 'string']]));
  41. $course = Course::create( $request->validate([
  42. 'code' => ['required','unique:courses','size:8','regex:/[A-Za-z]{4}[0-9]{4}/', 'string'],
  43. 'title' => ['required','max:255', 'string'],
  44. 'dept_id' => ['required','exists:departments,id', 'integer'],
  45. 'syllabus' => ['nullable','file', 'mimes:pdf']
  46. ]));
  47. if ($request->file('syllabus')) {
  48. $path = $request->file('syllabus')->store('course_syllabus', 'public');
  49. $course->update(['syllabus' => asset($path)]);
  50. }
  51. return redirect('/course/' . $course->id);
  52. }
  53. /**
  54. * Display the specified resource.
  55. *
  56. * @param \App\Course $course
  57. * @return \Illuminate\Http\Response
  58. */
  59. public function show(Course $course)
  60. {
  61. // dd($course->toJson());
  62. // dump(1);
  63. // Auth::logout();
  64. // $semesters = $course->semesters->unique()->loadMissing('sections');
  65. // $course->loadMissing('semesters.sections');
  66. // dd($semesters);
  67. $course->loadMissing(['semesters.sections.professors']);
  68. // dd($course->semesters->unique());
  69. // $semesters = $course->sections->groupBy('semester_code')->map( function ($sections) {
  70. // return collect(['alpha' => $sections[0]->semester->alpha, 'sections' => $sections->sortBy('code')]);
  71. // })->sort();
  72. // dd($semesters);
  73. // dd($course->loadMissing(['sections.semester']));
  74. // dd($course->sections->groupBy('semester_code'));
  75. return view('courses.show', [
  76. 'course' => $course,
  77. // 'semesters' => $semesters
  78. ]);
  79. }
  80. /**
  81. * Show the form for editing the specified resource.
  82. *
  83. * @param \App\Course $course
  84. * @return \Illuminate\Http\Response
  85. */
  86. public function edit(Course $course)
  87. {
  88. //
  89. }
  90. /**
  91. * Update the specified resource in storage.
  92. *
  93. * @param \Illuminate\Http\Request $request
  94. * @param \App\Course $course
  95. * @return \Illuminate\Http\Response
  96. */
  97. public function update(Request $request, Course $course)
  98. {
  99. $course->update( $request->validate([
  100. 'code' => ['unique:courses','size:8','regex:/[A-Za-z]{4}[0-9]{4}/', 'string'],
  101. 'title' => ['max:255', 'string'],
  102. 'dept_id' => ['exists:departments,id', 'integer'],
  103. 'syllabus' => ['nullable', 'file', 'mimetypes:application/pdf'],
  104. ]));
  105. if ($request->file('syllabus')) {
  106. $path = $request->file('syllabus')->store('course_syllabus');
  107. $course->update(['syllabus' => asset($path)]);
  108. }
  109. dump(asset($path));
  110. // dd($course->syllabus);
  111. return redirect($course->syllabus);
  112. return redirect('/course');
  113. }
  114. /**
  115. * Remove the specified resource from storage.
  116. *
  117. * @param \App\Course $course
  118. * @return \Illuminate\Http\Response
  119. */
  120. public function destroy(Course $course)
  121. {
  122. //
  123. }
  124. }