WIP edits

This commit is contained in:
Alexander Yakovlev 2022-09-19 09:57:59 +07:00
parent 8689a6a115
commit 5c68e3e44e
Signed by: oreolek
GPG key ID: 8D24103F5EE2A6C0
4 changed files with 19 additions and 4 deletions

View file

@ -3,6 +3,7 @@
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
@ -22,12 +23,24 @@ class PostController extends Controller
'item' => $post
]);
}
public function edit($id) {
public function edit($id, Request $request) {
if (is_numeric($id)) {
$post = Post::findOrFail($id);
} else {
$post = Post::where('slug', $id)->findOrFail();
}
if ($request->getMethod() === 'POST') {
$validated = $request->validate([
'content' => 'required|unique:posts',
'slug' => 'string|max:255|unique:posts',
'is_draft' => 'int|max:1',
'title' => 'required|string|max:255|unique:posts',
'password' => 'string|max:255',
]);
$post->fill($validated);
$post->save();
$request->session()->flash('success', _('Edit saved.'));
}
return view('edit', [
'item' => $post
]);

View file

@ -2,4 +2,5 @@
return [
'Tags:' => 'Метки:',
'Edit saved.' => 'Правка сохранена.',
];

View file

@ -2,6 +2,7 @@
@section('content')
<form method="POST">
@csrf
@if ($item->id)
<input type="hidden" name="post_id" value="{{ $item->id }}">
@endif

View file

@ -24,7 +24,7 @@ Route::controller(PageController::class)->group(function() {
Route::get('/page/index', 'index')->name('page.index');
Route::get('/page/drafts', 'drafts')->name('page.drafts');
Route::get('/page/view/{id}', 'view')->name('page.view');
Route::get('/page/edit/{id}', 'edit')->name('page.edit');
Route::any('/page/edit/{id}', 'edit')->name('page.edit');
});
Route::controller(TagController::class)->group(function() {
Route::get('/tag/index', [TagController::class, 'index'])->name('tag.index');
@ -32,7 +32,7 @@ Route::controller(TagController::class)->group(function() {
});
Route::controller(CommentController::class)->group(function() {
Route::get('/comment/index', 'index')->name('comments.index');
Route::any('/comment/load/{id}', 'load')->name('comments.load');
Route::get('/comment/load/{id}', 'load')->name('comments.load');
Route::any('/comment/edit/{id}', 'edit')->name('comments.edit');
Route::any('/comment/delete/{id}', 'delete')->name('comments.delete');
Route::any('/comment/post/{id}', 'post')->name('comments.post');
@ -42,6 +42,6 @@ Route::controller(PostController::class)->group(function() {
Route::get('/post/read', 'read')->name('post.read');
Route::any('/post/read/page/{page}', 'read');
Route::get('/post/view/{id}', 'view')->name('view');
Route::get('/post/edit/{id}', 'edit')->name('post.edit');
Route::any('/post/edit/{id}', 'edit')->name('post.edit');
Route::get('/{id}', 'view');
});