CourseSectionsController.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\CourseBook;
  4. use App\Models\CourseSection;
  5. use Illuminate\Http\Request;
  6. use App\Http\Controllers\Controller;
  7. use App\Http\Requests\CourseSectionRequest;
  8. class CourseSectionsController extends Controller
  9. {
  10. public function __construct()
  11. {
  12. $this->middleware('auth', ['except' => ['index', 'show']]);
  13. }
  14. public function index(CourseBook $book)
  15. {
  16. $sections = $book->sections()->paginate();
  17. return view('pages.course_sections.index', compact('book', 'sections'));
  18. }
  19. public function create(CourseBook $book, CourseSection $section)
  20. {
  21. return view('pages.course_sections.create_and_edit', compact('book', 'section'));
  22. }
  23. public function store(CourseSectionRequest $request, CourseBook $book, CourseSection $section)
  24. {
  25. $section->fill($request->all());
  26. $section->course_book_id = $book->id;
  27. $section->save();
  28. return redirect()->route('course.sections.index', $book->id)->with('message', '创建成功.');
  29. }
  30. public function edit(CourseBook $book, CourseSection $section)
  31. {
  32. $this->authorize('admin', $section);
  33. return view('pages.course_sections.create_and_edit', compact('book', 'section'));
  34. }
  35. public function update(CourseSectionRequest $request,CourseBook $book, CourseSection $section)
  36. {
  37. $this->authorize('admin', $section);
  38. $section->update($request->all());
  39. $section->fill($request->all());
  40. $section->course_book_id = $book->id;
  41. $section->save();
  42. return redirect()->route('course.sections.index', $book->id)->with('message', '更新成功.');
  43. }
  44. public function destroy(CourseBook $book, CourseSection $section)
  45. {
  46. $this->authorize('admin', $section);
  47. $section->delete();
  48. return redirect()->route('course.sections.index', $book->id)->with('message', '删除成功.');
  49. }
  50. }