Deleting a Post – WordPress-Like Blog Laravel 5.7 and AdminLTE 3 (14)

WordPress-Like Blog Laravel 5.7 and AdminLTE 3 (14) – Deleting a Post

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

  1. Delete an Existing Post and put it in the Trash
  2. List Deleted Posts inside the Trash
  3. Delete image and image thumbnail

Delete and Existing Post

To delete a post, firstly we’ll modify resources/views/backend/blog index.blade.php and add Form open and Form close around this line of code :

Next, let’s add some code inside destroy() method of Backend\BlogController.php

Let’s try to delete one post from the index page :

Membuat Blog dengan Laravel 5.7 dan AdminLTE 3 (14) – Menghapus Post

This action will cause the post deleted from the database.

Instead of directly delete the post from the database, we will modify the code in order to put a staging area for the post before it permanently deleted. We need to add the soft deletion method to posts table. Create a migration from the terminal :

Run the migration, add this line to app\Post.php

Now, if you delete the post, it will not disappear from the database table. It stays there with deleted_at field added instead.

We will put the deleted posts inside the trash. We will be able to undo deleted post and put it back to its original state. Modify destroy() method inside Backend\BlogController.php

Add message.blade.php inside backend/blog and move the message part from index.blade.php inside it :

And put this inside the empty line on index.blade.php :

Add new route to routes/web.php

Modify Backend\BlogController.php and add restore() method :

Now, if we delete a post, a notification and Undo button will appear. If you push it, the post will not be deleted.

Membuat Blog dengan Laravel 5.7 dan AdminLTE 3 (14) – Menghapus Post 2

List Deleted Posts Inside the Trash

The deleted posts will be displayed. Let’s modify index.blade.php beforehand :

Open Backend\BlogController.php and modify index() method :

Modify index.blade.php, cut the code from <table> to </table> , create a new file table.blade.php and paste the code inside it :

Create another file, table-trash.blade.php :

Since we mentioned force-destroy route, we need to add it to routes/web.php

Add forceDestroy() method inside Backend\BlogController.php

Delete Image and Image Thumbnail

Previously, we have successfully delete a post. However, if there was image for that post, we still haven’t delete it correctly. We will fix that problem.

Modify Backend\BlogController.php and create a new method, removeImage() :

And also add this method to forceDestroy :

Also modify update() method :

Now we will fix the paging. Modify index.blade.php on the $post->render() part into this :

Afterwards, we will create a menu between published and trash link on the top right corner. Modify index.blade.php :

Modify index() and restore() method inside Backend\BlogController.php

Let’s create those scope inside app\Post.php

Membuat Blog dengan Laravel 5.7 dan AdminLTE 3 (14) – Menghapus Post 3

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.