Managing Categories – WordPress-Like Blog Laravel 5.7 and AdminLTE 3 (15)

WordPress-Like Blog Laravel 5.7 and AdminLTE 3 (15) – Managing Categories

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

  1. Add Category Menu
  2. Display Categories
  3. Add Category
  4. Edit Category
  5. Delete Category

Add Category Menu

To add category menu on the sidebar, let’s modify sidebar.blade.php inside resources/views/layouts/backend directory.

Since the route doesn’t exist, let’s create it inside routes/web.php :

Display Categories

Create CategoryController from terminal :

Change the parent class to BackendController

Add these lines to the index() method :

Go to resources/views/backend directory, add a new folder ‘category’. Add index.blade.php inside this category.

Inside resources/views/backend, create another directory, ‘partials’, to put all files used by all the views so we don’t need to duplicate it everytime we create another view.

Add table.blade.php inside category folder :

Open the category index page :

Membuat Blog dengan Laravel 5.7 dan AdminLTE 3 (15) – Manage Kategori

Add Category

Add new method create() to CategoryController.php

Add create.blade.php inside resources/views/backend/category folder :

Inside the same directory, add form.blade.php :

Also add script.blade.php :

Next, we need to deal with store() method inside CategoryController.php, previously, let’s create another request, CategoryStoreRequest and CategoryUpdateRequest :

Store method on CategoryController :

Don’t forget to add namespace :

Add fillable and table property inside App\Category model :

Now you can add a new category :

Membuat Blog dengan Laravel 5.7 dan AdminLTE 3 (15) – Manage Kategori

Add a new Category, ‘Uncategorized’. This category will be useful later when we delete any category.

Edit Category

To edit the category, modify edit() method inside CategoryController :

Modify CategoryUpdateRequest :

Add some modification to update() method inside CategoryController :

Add edit.blade.php inside backend/category folder :

Now you will be able to edit the category name :

Membuat Blog dengan Laravel 5.7 dan AdminLTE 3 (15) – Manage Kategori 3

Delete Category

To delete a category, modify the destroy() method inside CategoryController :

However, we wouldn’t be able to delete this category since it’s restricted. That’s why we made one ‘uncategorized’ category before, so the post that have the same category with the deleted category will be automatically under uncategorized category.

Let’s protect the uncategorized category on the client and server side so it couldn’t be deleted.

In my categories table, uncategorized has an id ‘7’. Open config/cms.php

Next, create a new request, CategoryDestroyRequest

We have set the client side. Now for the client side, we will create a disabled button for the uncategorized category. Open table.blade.php inside category folder and modify this lines :

Membuat Blog dengan Laravel 5.7 dan AdminLTE 3 (15) – Manage Kategori 4

Modify destroy method inside CategoryController :

And add post model namespace :

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.