Laravel CMS | N-07 | Add New Post

Video Tutorial


So if you have been following the CMS Series this Far, you probably thinking about how to insert posts in the database so you can show them on your blog or another web app.

So in this tutorial, we are going to walk through the guide on how to Use a form to insert data into the database.

Please Make sure to watch/read the last tutorials so at least you know how things works

Adding Posts

So the first thing we need is an additional new post file so we are going to name it addNewPost.blade.php we put the form for adding new posts you can use normal forms, but i’m going to implement it using Laravel Collective only for fast integration and simplicity, so the form should be very simple

{!! Form::open(['route' => 'post.add', 'method' => 'POST']) !!}

    <div class="form-group">
        {!! Form::label('postTitle', 'Post Title') !!}

        {!! Form::text('postTitle', '', ['class' => 'form-control']) !!}

    <div class="form-group">
        {!! Form::label('postCategory', 'Post Category') !!}

        <select name="postCategory" id="postCategory" class="form-control">
            <option>No Category</option>
            @foreach($categories as $category)
            <option>{{ $category->name }}</option>


    <div class="form-group">

        {!! Form::label('postContent', 'Post Content') !!}

        {!! Form::textarea('postContent', '', ['class' => 'form-control']) !!}


    <div class="form-group">

        {!! Form::submit('Add Post', ['class' => 'btn btn-success btn-block']) !!}


{!! Form::close() !!}

Now we need to set up the route for redirecting us to the add new post page, just add

//Make Sure To Create the PostConroller 
Route::get('/addNewPost', 'PostController@index')->name('post.index');

under Web.php, make Sure to create the PostController.

also in the index page render the view

public function index() {
   return view('pages.addNewPost');

Actually, Set it up as we always do.

Add another Route for handling the Post Request Submitted by the Add New Post Form

//Set the Route argument under the Collective Form to the post.add
Route::post('/addPost', 'PostController@store')->name('post.add')

So it is time for Storing the Post Data Submitted by the Admin into the database, we also need to run data checking for making sure that the Data is Valid.

public function store(Request $request)
    //We Validate The Request (for e.x: the post title should not be empty)
    $this->validate($request, ['postTitle' => 'required|min:3|max:200', 'postCategory' => 'required', 'postContent' => 'required']);

    $post = new Post();
    $post->title = $request->postTitle;
    $post->author = "Islem Penywis";
    $post->category = $request->postCategory;
    $post->post = $request->postContent;
    //Save Intance 

    session()->flash('Success', 'Post Added Successfully');
    return back();

as you may have noticed we use the Protected Function validate() function for validating the Request.

All we do is set the Fields and Save the Instance in the Database.

And now the Post Should get added via the Form, But still, we won’t be able to see the available posts, therefore what you can do is show a table containing the Available Posts the same as with listing the available Categories.

What is Next

So that was it for adding posts, now for managing posts, you may need to use the same procedure by adding buttons for Editing and Removing the Post by showing a popup

Get the Full Source code on Github

No Comments Yet