| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 | 
							- <?php
 
- namespace App\Http\Controllers;
 
- use Illuminate\Http\Request;
 
- use App\Models\Author;
 
- class AuthorController extends Controller {
 
- 	function index() {
 
- 		$authors = Author::all();
 
- 		return view("authors", ["rows" => $authors]);
 
- 	}
 
- 	function add() {
 
- 		return view("add_author_form");
 
- 	}
 
- 	function view(Author $author) {
 
- 		return view("author", [
 
- 			"author" => $author->load([
 
- 				"books" => function($query) {},
 
- 				"comments" => function($query) { $query->recent(); }
 
- 			])
 
- 		]);
 
- 	}
 
- 	function edit(Author $author) {
 
- 		return view("edit_author_form", ["author" => $author]);
 
- 	}
 
- 	function store(Request $request) {
 
- 		$request->validate([
 
- 			"id" => "nullable|exists:authors",
 
- 			"name" => "required",
 
- 			"description" => "nullable"
 
- 		], [
 
- 			"name" => "Автор должен иметь имя."
 
- 		]);
 
- 		$arr = $request;
 
- 		$author = Author::find($arr->id) ?? new Author;
 
- 		$author->name = $arr->name;
 
- 		$author->description = $arr->description;
 
- 		$author->save();
 
- 		return view("success");
 
- 	}
 
- 	function comment(Author $author, Request $request) {
 
- 		$request->validate([
 
- 			"name" => "required",
 
- 			"email" => "required|email",
 
- 			"content" => "required"
 
- 		], [
 
- 			"name" => "Укажите ваше имя.",
 
- 			"email" => "Укажите ваш email.",
 
- 			"content" => "Введите комментарий."
 
- 		]);
 
- 		$arr = $request;
 
- 		$author->comments()->create([
 
- 			"name" => $arr->name,
 
- 			"email" => $arr->email,
 
- 			"content" => $arr->content
 
- 		]);
 
- 		return view("success");
 
- 	}
 
- 	function drop(Author $author) {
 
- 		foreach($author->books as $book) {
 
- 			$book->comments()->delete();
 
- 			$book->delete();
 
- 		}
 
- 		$author->delete();
 
- 		return view("success");
 
- 	}
 
- }
 
 
  |