Post Categories and Markdown – WordPress-Like Blog Laravel 5.7 and AdminLTE 3 (6)

WordPress-Like Blog Laravel 5.7 and AdminLTE 3 (6) – Post Categories and Markdown

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

  1. Enable Markdown Support
  2. Create the Categories of the posts

Enable Markdown Support

Markdown is a lightweight markup language with plain text formatting syntax. It is designed so that it can be converted to HTML and many other formats using a tool by the same name. Markdown is used in most readme.md file when you open github. We will use markdown on our project to be used in the post body.

Tool to translate markdown into html, we will use LaravelMarkdown. Open the terminal and type the command :

After installation has finished, open config/app.php and insert these two lines under providers and aliases.

Next, open show.blade.php, on the $post->body part, change into the following syntax.

{!! !!}} means renders directly to html.

Before we run the url, add this method to app\Post.php

Don’t forget to add the markdown namespace :

Open index.blade.php and insert the excerpt_html method as well :

Open localhost:8000, there will be no errors occured, you have successfully implemented markdown support.

Post Categories

We will add master categories and relate it to the post model.

First step we have to do is create the categories table.

Create another migration to add a new column in the post table.

Let’s create a new seeder, CategoriesTableSeeder :

Add the CategoriesTableSeeder to DatabaseSeeder.php :

Next, open config/database.php and change ‘strict’ to false :

Run the migration and seed :

Create the category Model :

Define the relationship to post model, also add getRouteKeyName method to filter by categories later.

Open Post model and define a relationship to category model :

Next, we will display the categories on the sidebar. Previously we need to modify BlogController action index() and add category() method, don’t forget to add use App\Category.

 

Add a new route in routes/web.php

Add this category widget in views/layouts/sidebar.blade.php

Now, we will create an alert to give information in which category we are in the page :

Open index.blade.php and add alert :

Membuat Blog dengan Laravel 5.6 dan AdminLTE 3 (6) – Post Kategori dan Markdown

There will be an error if we click the blog title right now. We will modify some codes to prevent this error and code duplication. In BlogController currently we have duplication on how to get the categories in every method. We will be using view composer to tidy things up.

Let’s create a new provider. Open terminal and type the command :

All necessary codes will be loaded in this Provider. Open the newly created file in the App\providers directory, don’t forget to use App\Category :

We need to register this provider to config/app.php

Now let’s clean up our BlogController.php. The end result :

php artisan serve and open the project, everything should be working perfectly now.

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.