Create a New Post – WordPress-Like Blog Laravel 5.7 and AdminLTE 3 (11)

WordPress-Like Blog Laravel 5.7 and AdminLTE 3 (11) – Create a New Post

In this eleventh part of creating WordPress-Like Blog using Laravel 5.7 and AdminLTE 3, we will :

  1. Create Form to Add a New Post
  2. Create Form Validation
  3. Image Upload
  4. Create Image Thumbnail

Create Form to Add a New Post

We will install laravelcollective to make our job to create form easier. Open terminal and install laravelcollective :

When the installation has finished, open config/app.php and add to ‘Provider’ and ‘Aliases’ part :

Ok now we’re good to go.

Open Backend\BlogController.php and perform modification to create() method :

Create a new file, ‘create.blade.php’ inside resources/views/backend/blog directory :

Open the backend project url, and click Add New Post on the sidebar menu :

Membuat Blog dengan Laravel 5.7 dan AdminLTE 3 (11) – Membuat Post Baru

Create Form Validation

Now we will create a validation rule before the form is submitted. For example, which field that required to be filled with data, the correct format date, etc. Let’s begin by adding some code to Backend\BlogController.php store() method :

Here we also add handleRequest() method to cope with image upload that we will create later.

Create new request, the PostRequest by typing the command on the terminal :

Open the newly PostRequest.php inside app\Http\Requests folder. Change the authorize() method inside PostRequest to true, and add the rules method :

Don’t forget to add the namespace :

Try to Submit the form, error message will occured :

Membuat Blog dengan Laravel 5.7 dan AdminLTE 3 (11) – Membuat Post Baru 2

Before we submit the form with valid data, there is one step we need to do, that is to add $fillable property inside Post model :

And add a new method :

Image Upload

To upload an image, first we need to modify create.blade.php to tell the form multipart/form-data attribute :

Add new property uploadPath along with construct() method inside Backend\BlogController.php

Add a new folder ‘img’ inside public folder.

Let’s try to fill the form with valid data and push Submit button.

Membuat Blog dengan Laravel 5.7 dan AdminLTE 3 (11) – Membuat Post Baru 3

Create Image Thumbnail

After we successfully add a post with image, now we will create the image thumbnail automatically everytime a post with image is being submitted. We need a third party package, the intervention/image package.

When installation has finished, open config/app.php and add the following lines to Providers and Aliases section respectively.

Then run the configuration from the terminal :

Next, modify the handleRequest() method inside Backend\BlogController :

Create new config file, cms.php inside config folder :

Modify these two methods on Post.php

Hope this one doesn’t too long enough.

Github Commit.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.