Skip to content

Commit

Permalink
add single post view
Browse files Browse the repository at this point in the history
  • Loading branch information
levijackson committed Jun 25, 2021
1 parent f9684de commit e66c4d2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
12 changes: 12 additions & 0 deletions app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ public function create(Request $request)
}
}

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

if ($post) {
return view('blog.posts.single', ['post' => $post]);
}

return redirect('/')
->withErrors('This post does not exist.');
}

public function save(Request $request)
{
$post = new Post();
Expand Down
7 changes: 6 additions & 1 deletion resources/views/blog/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
@section('content')
<h1>{{$title}}</h1>
@forelse ($posts as $post)
<li>{{ $post->title }}</li>
@guest
<li>{{ $post->title }}</li>
@endguest
@auth
<li>{{ $post->title }} - <a href="/blog/post/{{ $post->slug }}">View</a> | <a href="/admin/blog/post/{{ $post->slug }}">Edit</a></li>
@endauth
@empty
<p>No posts</p>
@endforelse
Expand Down
7 changes: 7 additions & 0 deletions resources/views/blog/posts/single.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@extends('layouts.app')

@section('content')
<h1>{{ $post->title }}</h1>

<p>{{ $post->body }}</p>
@stop
5 changes: 4 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

Route::get('/blog', 'App\Http\Controllers\PostController@index');
Route::group(['prefix' => 'blog'], function () {
Route::get('/', 'App\Http\Controllers\PostController@index');
Route::get('/post/{slug}', 'App\Http\Controllers\PostController@single');
});

Auth::routes();

Expand Down

0 comments on commit e66c4d2

Please sign in to comment.