Explorar o código

Article editing + deletion

Highlights: reusing one form for both creation and editing
axkuhta hai 1 ano
pai
achega
73542bd6cb

+ 9 - 0
app/Http/Controllers/ArticleController.php

@@ -21,6 +21,10 @@ class ArticleController extends Controller
 		]);
 	}
 
+	function edit(Article $article) {
+		return view("edit_article_form", ["article" => $article]);
+	}
+
 	function store(Request $request) {
 		$request->validate([
 			"id" => "nullable|exists:articles",
@@ -49,4 +53,9 @@ class ArticleController extends Controller
 		return view("success");
 	}
 
+	function drop(Article $article) {
+		$article->delete();
+
+		return view("success");
+	}
 }

+ 1 - 67
resources/views/add_article_form.blade.php

@@ -2,71 +2,5 @@
 
 @section("content")
 <h1>Добавление публикации</h1>
-<form method="POST" action="">
-	@csrf
-
-	<div>
-		<label>
-			<div>Название:</div>
-			<input type="text" name="title" placeholder="Название..." value="{{ old("title") }}">
-			@error("title")
-				<span class="alert">{{ $message }}</span>
-			@enderror
-		</label>
-	</div>
-
-	<div>
-		<label>
-			<div>Короткое описание:</div>
-			<textarea name="description" class="article-description-textarea" placeholder="Если оставить это поле пустым, то короткое описание будет сгенерировано автоматически из первого предложения публикации...">{{ old("description") }}</textarea>
-			@error("description")
-				<span class="alert">{{ $message }}</span>
-			@enderror
-		</label>
-	</div>
-
-	<div>
-		<label>
-			<div>Текст публикации:</div>
-			<textarea name="content" class="article-content-textarea" placeholder="Текст публикации...">{{ old("content") }}</textarea>
-			@error("content")
-				<span class="alert">{{ $message }}</span>
-			@enderror
-		</label>
-	</div>
-
-	<div>
-		<label>
-			<div>Состояние:</div>
-			<select name="status" value="{{ old("status") }}">
-				<option value="0">Aaaaa</option>
-			</select>
-			@error("status")
-				<span class="alert">{{ $message }}</span>
-			@enderror
-		</label>
-	</div>
-
-	<div>
-		<label>
-			<div>Дата и время публикации:</div>
-			<input type="datetime-local" name="publish_at" value="{{ old("publish_at") }}">
-			@error("publish_at")
-				<span class="alert">{{ $message }}</span>
-			@enderror
-		</label>
-	</div>
-
-	<div>
-		<label>
-			<div>Дата и время снятия с публикации:</div>
-			<input type="datetime-local" name="unpublish_at" value="{{ old("unpublish_at") }}">
-			@error("unpublish_at")
-				<span class="alert">{{ $message }}</span>
-			@enderror
-		</label>
-	</div>
-
-	<input type="submit">
-</form>
+@include("forms.article_form")
 @endsection

+ 6 - 0
resources/views/edit_article_form.blade.php

@@ -0,0 +1,6 @@
+@extends("layouts.app")
+
+@section("content")
+<h1>Редактирование публикации</h1>
+@include("forms.article_form", ["article" => $article])
+@endsection

+ 69 - 0
resources/views/forms/article_form.blade.php

@@ -0,0 +1,69 @@
+<form method="POST" action="">
+	@csrf
+
+	<input type="hidden" name="id" value="{{ $article->id ?? null }}">
+
+	<div>
+		<label>
+			<div>Название:</div>
+			<input type="text" name="title" placeholder="Название..." value="{{ old("title") ?? $article->title ?? null }}">
+			@error("title")
+				<span class="alert">{{ $message }}</span>
+			@enderror
+		</label>
+	</div>
+
+	<div>
+		<label>
+			<div>Короткое описание:</div>
+			<textarea name="description" class="article-description-textarea" placeholder="Если оставить это поле пустым, то короткое описание будет сгенерировано автоматически из первого предложения публикации...">{{ old("description") ?? $article->description ?? null }}</textarea>
+			@error("description")
+				<span class="alert">{{ $message }}</span>
+			@enderror
+		</label>
+	</div>
+
+	<div>
+		<label>
+			<div>Текст публикации:</div>
+			<textarea name="content" class="article-content-textarea" placeholder="Текст публикации...">{{ old("content") ?? $article->content ?? null }}</textarea>
+			@error("content")
+				<span class="alert">{{ $message }}</span>
+			@enderror
+		</label>
+	</div>
+
+	<div>
+		<label>
+			<div>Состояние:</div>
+			<select name="status" value="{{ old("status") ?? $article->status ?? null }}">
+				<option value="0">Aaaaa</option>
+			</select>
+			@error("status")
+				<span class="alert">{{ $message }}</span>
+			@enderror
+		</label>
+	</div>
+
+	<div>
+		<label>
+			<div>Дата и время публикации:</div>
+			<input type="datetime-local" name="publish_at" value="{{ old("publish_at") ?? $article->publish_at ?? null }}">
+			@error("publish_at")
+				<span class="alert">{{ $message }}</span>
+			@enderror
+		</label>
+	</div>
+
+	<div>
+		<label>
+			<div>Дата и время снятия с публикации:</div>
+			<input type="datetime-local" name="unpublish_at" value="{{ old("unpublish_at") ?? $article->unpublish_at ?? null }}">
+			@error("unpublish_at")
+				<span class="alert">{{ $message }}</span>
+			@enderror
+		</label>
+	</div>
+
+	<input type="submit">
+</form>

+ 4 - 1
routes/web.php

@@ -47,5 +47,8 @@ Route::get('/api/books', function() { return Resources\BookResource::collection(
 // Публикации
 Route::get('/articles', [Controllers\ArticleController::class, 'index']);
 Route::get('/article/add', [Controllers\ArticleController::class, 'add']);
-Route::post('/article/add', [Controllers\ArticleController::class, 'store']);
 Route::get('/article/{article}', [Controllers\ArticleController::class, 'view']);
+Route::get('/article/{article}/delete', [Controllers\ArticleController::class, 'drop']);
+Route::get('/article/{article}/edit', [Controllers\ArticleController::class, 'edit']);
+Route::post('/article/{article}/edit', [Controllers\ArticleController::class, 'store']);
+Route::post('/article/add', [Controllers\ArticleController::class, 'store']);