Skip to content

Commit

Permalink
adding routes to edit or delete a post
Browse files Browse the repository at this point in the history
  • Loading branch information
levijackson committed Jun 25, 2021
1 parent b6dd99a commit 3ef54dd
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
38 changes: 38 additions & 0 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,42 @@ public function save(Request $request)
return redirect('admin/blog/post/' . $post->slug)
->withMessage($message);
}

public function edit(Request $request, string $slug)
{
$post = Post::where('slug', $slug)->first();

if ($post && ($request->user()->id == $post->user_id || $request->user()->isAdmin())) {
return view('blog.posts.edit', ['post' => $post]);
}

return redirect('/')
->withErrors('You do not have permission to edit this post.');
}

public function update(Request $request, string $slug)
{
$post = Post::where('slug', $slug)->first();

if ($request->has('delete')) {
$post->delete();
$message = 'Post deleted!';
return redirect('admin/blog/post/')
->withMessage($message);
}

$post->title = $request->get('title');
$post->metaTitle = $request->get('metaTitle');
$post->body = $request->get('body');
$post->metaDescription = $request->get('metaDescription');
$post->slug = $request->get('urlSlug') ?? Str::slug($post->title);
$post->user_id = $request->user()->id;

$message = 'Post updated!';

$post->save();

return redirect('admin/blog/post/' . $post->slug)
->withMessage($message);
}
}
38 changes: 38 additions & 0 deletions resources/views/blog/posts/edit.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
@extends('layouts.app')

@section('content')
@if (session('message'))
<div class="alert alert-success">
{{ session('message') }}
</div>
@endif

<form action="/admin/blog/post/{{ $post->slug }}" method="post" class="container">
<input type="hidden" name="_method" value="PUT">

<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="title" class="form-control" value={{ $post->title }} />
</div>
<div class="form-group">
<label for="metaTitle">Meta Title</label>
<input type="text" name="metaTitle" class="form-control" value={{ $post->metaTitle }} />
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea name="body" class="form-control">{{ $post->body }}</textarea>
</div>
<div class="form-group">
<label for="metaDescription">Meta Description</label>
<textarea name="metaDescription" class="form-control">{{ $post->metaDescription }}</textarea>
</div>
<div class="form-group">
<label for="urlSlug">URL Slug (optional)</label>
<small>If not specified one will be created from the title</small>
<input type="text" name="urlSlug" class="form-control" value={{ $post->slug }} />
</div>
<input type="submit" name="update" class="btn btn-success" value="Update" />
<input type="submit" name="delete" class="btn btn-danger" value="Delete" />
</form>
@stop
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@
Route::group(['prefix' => 'blog'], function () {
Route::get('/post', 'App\Http\Controllers\PostController@create');
Route::post('/post', 'App\Http\Controllers\PostController@save');
Route::get('/post/{slug}', 'App\Http\Controllers\PostController@edit');
Route::put('/post/{slug}', 'App\Http\Controllers\PostController@update');
});
});

0 comments on commit 3ef54dd

Please sign in to comment.