浏览代码

Author editing

axkuhta 1 年之前
父节点
当前提交
8eb4f77483

+ 6 - 1
app/Http/Controllers/AuthorController.php

@@ -20,8 +20,13 @@ class AuthorController extends Controller {
 		return view("author", ["author" => $author]);
 	}
 
+	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"
 		], [
@@ -30,7 +35,7 @@ class AuthorController extends Controller {
 
 		$arr = $request;
 
-		$author = new Author;
+		$author = Author::find($arr->id) ?? new Author;
 		$author->name = $arr->name;
 		$author->description = $arr->description;
 		$author->save();

+ 3 - 0
resources/views/author.blade.php

@@ -6,4 +6,7 @@
 <div>Описание автора:</div>
 <div>{{$author->description ?? "N/A"}}</div>
 </p>
+<p>
+<a href="/author/{{ $author->id }}/edit">Редактировать</a>
+</p>
 @endsection

+ 32 - 0
resources/views/edit_author_form.blade.php

@@ -0,0 +1,32 @@
+@extends("layouts.app")
+
+@section("content")
+<h1>Редактирование автора</h1>
+<form method="POST" action="">
+	@csrf
+
+	<input type="hidden" name="id" value="{{ $author->id }}">
+
+	<div>
+		<label>
+			<div>Имя:</div>
+			<input type="text" name="name" placeholder="Имя автора..." value="{{ old("name") ?? $author->name }}">
+			@error("name")
+				<span class="alert">{{ $message }}</span>
+			@enderror
+		</label>
+	</div>
+
+	<div>
+		<label>
+			<div>Описание:</div>
+			<textarea name="description" placeholder="Описание...">{{ old("description") ?? $author->description }}</textarea>
+			@error("description")
+				<span class="alert">{{ $message }}</span>
+			@enderror
+		</label>
+	</div>
+
+	<input type="submit">
+</form>
+@endsection

+ 2 - 0
routes/web.php

@@ -30,4 +30,6 @@ Route::post('/book/add', [Controllers\BookController::class, 'store']);
 Route::get('/authors', [Controllers\AuthorController::class, 'index']);
 Route::get('/author/add', [Controllers\AuthorController::class, 'add']);
 Route::get('/author/{author}', [Controllers\AuthorController::class, 'view']);
+Route::get('/author/{author}/edit', [Controllers\AuthorController::class, 'edit']);
+Route::post('/author/{author}/edit', [Controllers\AuthorController::class, 'store']);
 Route::post('/author/add', [Controllers\AuthorController::class, 'store']);