Няма описание

ProfessorController.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Professor;
  4. use App\Semester;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Http\Request;
  7. class ProfessorController extends Controller
  8. {
  9. /**
  10. * Display a listing of the resource.
  11. *
  12. * @return \Illuminate\Http\Response
  13. */
  14. public function index()
  15. {
  16. // dd(Professor::where('dept_id', '=', 1)->with(['sections:semester_code,credits,student_count', 'semesters:semester_code,admin_load,investigative_load'])->orderBy('professors.last_name')->get());
  17. return view('professors.index', [
  18. 'professors' => Professor::where('dept_id', '=', 1)->with(['sections:semester_code,credits,student_count', 'semesters:semester_code,admin_load,investigative_load'])->orderBy('professors.last_name')->get(),
  19. 'semesters' => Semester::orderBy('semesters.code', 'asc')->get(),
  20. ]);
  21. }
  22. /**
  23. * Show the form for creating a new resource.
  24. *
  25. * @return \Illuminate\Http\Response
  26. */
  27. public function create()
  28. {
  29. //
  30. }
  31. /**
  32. * Store a newly created resource in storage.
  33. *
  34. * @param \Illuminate\Http\Request $request
  35. * @return \Illuminate\Http\Response
  36. */
  37. public function store(Request $request)
  38. {
  39. //
  40. $professor = Professor::create( $request->validate([
  41. 'num_prof' => ['required', 'unique:professors', 'max:16', 'string'],
  42. 'first_name' => ['required', 'max:255', 'string', 'alpha'],
  43. 'last_name' => ['required', 'max:255', 'string', 'alpha'],
  44. 'email' => ['nullable', 'email'],
  45. 'type' => ['nullable', 'in:plantilla,contrato,ta'],
  46. 'dept_id' => ['required', 'exists:departments,id', 'integer'],
  47. ]));
  48. // dd($professor);
  49. return redirect('/professor/' . $professor->id);
  50. }
  51. /**
  52. * Display the specified resource.
  53. *
  54. * @param \App\Professor $professor
  55. * @return \Illuminate\Http\Response
  56. */
  57. public function show(Professor $professor)
  58. {
  59. $professor->loadMissing(['sections.course', 'semesters.sections.course']);
  60. $loads = $professor->semesters()->get()->keyBy('code');
  61. $sections = $professor->sections()->with('course:id,code')->get()->groupBy('semester_code');
  62. $semesters = Semester::whereIn('code', $loads->keys())->orWhereIn('code', $sections->keys())->get();
  63. return view('professors.show', compact([
  64. 'professor',
  65. 'semesters',
  66. 'sections',
  67. 'loads'
  68. ]));
  69. }
  70. /**
  71. * Show the form for editing the specified resource.
  72. *
  73. * @param \App\Professor $professor
  74. * @return \Illuminate\Http\Response
  75. */
  76. public function edit(Professor $professor)
  77. {
  78. //
  79. }
  80. /**
  81. * Update the specified resource in storage.
  82. *
  83. * @param \Illuminate\Http\Request $request
  84. * @param \App\Professor $professor
  85. * @return \Illuminate\Http\Response
  86. */
  87. public function update(Request $request, Professor $professor)
  88. {
  89. // TODO: Add logic for updating professor
  90. if ($request->isMethod('put')) {
  91. $data = array_filter($request->validate([
  92. 'semester_code' => ['required_with:admin_load,investigative_load', 'string','size:3'],
  93. 'admin_load' => ['nullable', 'numeric'],
  94. 'investigative_load' => ['nullable', 'numeric']
  95. ]));
  96. // dd($data);
  97. if (! $professor->semesters->contains($data['semester_code'])) {
  98. $professor->semesters()->attach($data['semester_code'], $data);
  99. } else {
  100. $professor->semesters()->updateExistingPivot($data['semester_code'], $data);
  101. }
  102. }
  103. return redirect()->back();
  104. }
  105. /**
  106. * Remove the specified resource from storage.
  107. *
  108. * @param \App\Professor $professor
  109. * @return \Illuminate\Http\Response
  110. */
  111. public function destroy(Professor $professor)
  112. {
  113. //
  114. }
  115. }