AuthorController.php 978 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Author;
  5. class AuthorController extends Controller {
  6. function index() {
  7. $authors = Author::all();
  8. return view("authors", ["rows" => $authors]);
  9. }
  10. function add() {
  11. return view("add_author_form");
  12. }
  13. function view(Author $author) {
  14. return view("author", ["author" => $author]);
  15. }
  16. function edit(Author $author) {
  17. return view("edit_author_form", ["author" => $author]);
  18. }
  19. function store(Request $request) {
  20. $request->validate([
  21. "id" => "nullable|exists:authors",
  22. "name" => "required",
  23. "description" => "nullable"
  24. ], [
  25. "name" => "Автор должен иметь имя."
  26. ]);
  27. $arr = $request;
  28. $author = Author::find($arr->id) ?? new Author;
  29. $author->name = $arr->name;
  30. $author->description = $arr->description;
  31. $author->save();
  32. return view("success");
  33. }
  34. function drop(Author $author) {
  35. $author->delete();
  36. return view("success");
  37. }
  38. }