Add blog post create

This commit is contained in:
Alex Thomassen 2020-03-11 12:57:13 +01:00
parent 60b4cdd8e9
commit 5370cfefeb
Signed by: Alex
GPG Key ID: 10BD786B5F6FF5DE
9 changed files with 111 additions and 11 deletions

View File

@ -5,7 +5,8 @@ namespace App\Http\Controllers;
use App\BlogPost; use App\BlogPost;
use App\User; use App\User;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Http\Request\BlogPostRequest; use App\Http\Requests\BlogPostRequest;
use Auth;
class BlogPostController extends Controller class BlogPostController extends Controller
{ {
@ -32,7 +33,7 @@ class BlogPostController extends Controller
*/ */
public function create() public function create()
{ {
// return view('posts.create');
} }
/** /**
@ -43,7 +44,18 @@ class BlogPostController extends Controller
*/ */
public function store(BlogPostRequest $request) public function store(BlogPostRequest $request)
{ {
// $validated = $request->validated();
$post = new BlogPost($validated);
$post->user_id = Auth::id();
$post->save();
$alert = [
'type' => 'success',
'message' => 'Successfully created and published your blog post!',
];
return view('posts.show', compact('post', 'alert'));
} }
/** /**
@ -54,7 +66,7 @@ class BlogPostController extends Controller
*/ */
public function show(BlogPost $post) public function show(BlogPost $post)
{ {
// return view('posts.show', compact('post'));
} }
/** /**

View File

@ -3,6 +3,7 @@
namespace App\Http\Requests; namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
use Auth;
class BlogPostRequest extends FormRequest class BlogPostRequest extends FormRequest
{ {
@ -25,8 +26,8 @@ class BlogPostRequest extends FormRequest
public function rules() public function rules()
{ {
return [ return [
'title' => 'required|max:150', 'title' => 'required|min:5|max:150',
'body' => 'required', 'body' => 'required|min:20|max:10000',
]; ];
} }

View File

@ -27,7 +27,7 @@ class UserSeeder extends Seeder
$user->name = $name; $user->name = $name;
$user->email = $username . '@example.com'; $user->email = $username . '@example.com';
$user->password = Hash::make('Kaffekopp123'); $user->password = Hash::make('Kaffekopp123');
$user->admin = true; $user->admin = $username === 'alex' ? true : false;
$user->save(); $user->save();
} }

View File

@ -34,6 +34,19 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent"> <div class="collapse navbar-collapse" id="navbarSupportedContent">
<!-- Left Side Of Navbar --> <!-- Left Side Of Navbar -->
<ul class="navbar-nav mr-auto"> <ul class="navbar-nav mr-auto">
@auth
<li class="nav-item dropdown">
<a id="postsDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
<i class="fas fa-pencil fa-fw" aria-hidden="true"></i> Posts <span class="caret"></span>
</a>
<div class="dropdown-menu" aria-labelledby="postsDropdown">
<a class="dropdown-item" href="{{ route('posts.create') }}">
<i class="fas fa-pencil-alt fa-fw" aria-hidden="true"></i> Create new post!
</a>
</div>
</li>
@endauth
</ul> </ul>
<!-- Right Side Of Navbar --> <!-- Right Side Of Navbar -->

View File

@ -0,0 +1,52 @@
@extends('layouts.app')
@section('content')
<div class="container">
<h1><i class="fas fa-pencil-alt fa-fw"></i> Create new post</h1>
<br>
<form action="{{ route('posts.store') }}" method="post">
@csrf
<div class="card">
<div class="form-group">
<h5 class="card-header text-white bg-primary" for="title">Post title:</h5>
<div class="card-body">
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="title-addon"><i class="fas fa-heading fa-fw"></i></span>
</div>
<input type="text" minlength="3" maxlength="150" class="form-control" name="title" id="title" aria-describedby="titleHelp" placeholder="Hello world!">
</div>
<small id="titleHelp" class="form-text text-muted">The title of your blog post. Be creative! <i class="far fa-smile-beam"></i></small>
</div>
</div>
</div>
<br>
<div class="card">
<div class="form-group">
<h5 class="card-header text-white bg-primary" for="body">Post content:</h5>
<div class="card-body">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fab fa-markdown fa-fw"></i></span>
</div>
<textarea class="form-control" minlength="20" maxlength="10000" aria-describedby="bodyHelp" name="body" id="body" rows="7"></textarea>
</div>
<small id="bodyHelp" class="form-text text-muted">The body of your blog post. Supports <i class="fab fa-markdown"></i> Markdown!</small>
</div>
</div>
</div>
<br>
<button class="btn btn-primary" type="submit" role="button"><i class="fas fa-share-square fa-fw"></i> Submit post!</button>
</form>
</div>
@endsection

View File

@ -0,0 +1,6 @@
<div class="card">
<h3 class="card-header bg-primary text-white">{{ $post->title }}</h3>
<div class="card-body">
@markdown($post->body)
</div>
</div>

View File

@ -9,11 +9,9 @@
@else @else
@foreach ($user->posts as $post) @foreach ($user->posts as $post)
<div class="jumbotron"> <div class="jumbotron">
<h1 class="user-post-heading">{{ $post->title }}</h1> @include('posts.display-template', $post)
<div class="user-post-body">
@markdown($post->body)
</div>
<br><br>
<div class="row align-items-center"> <div class="row align-items-center">
<span class="col-6 text-muted"> <span class="col-6 text-muted">
Created: {{ $post->created_at }} Created: {{ $post->created_at }}
@ -36,6 +34,7 @@
@auth @auth
@if (Auth::admin() && Auth::id() !== $user->id) @if (Auth::admin() && Auth::id() !== $user->id)
<br><br>
<div class="row"> <div class="row">
<span class="col align-self-end"> <span class="col align-self-end">
<div class="alert alert-danger"> <div class="alert alert-danger">

View File

@ -0,0 +1,13 @@
@extends('layouts.app')
@section('content')
<div class="container">
@if (isset($alert))
<div class="alert alert-{{ $alert['type'] }}">
{{ $alert['message'] }}
</div>
@endif
@include('posts.display-template')
</div>
@endsection

View File

@ -21,6 +21,10 @@ Route::get('/home', 'HomeController@index')->name('home');
Route::group(['prefix' => 'posts', 'as' => 'posts.'], function() { Route::group(['prefix' => 'posts', 'as' => 'posts.'], function() {
Route::get('/u/{username}', ['as' => 'index', 'uses' => 'BlogPostController@index']); Route::get('/u/{username}', ['as' => 'index', 'uses' => 'BlogPostController@index']);
Route::get('/p/{post}', ['as' => 'show', 'uses' => 'BlogPostController@show']);
Route::get('/create', ['as' => 'create', 'uses' => 'BlogPostController@create']);
Route::post('/create', ['as' => 'store', 'uses' => 'BlogPostController@store']);
Route::delete('/delete/{post}', ['as' => 'delete', 'uses' => 'BlogPostController@destroy']); Route::delete('/delete/{post}', ['as' => 'delete', 'uses' => 'BlogPostController@destroy']);
}); });