WordPress-Like Blog Laravel 5.6/5.7 and AdminLTE 3 (4) – Dynamic Data

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

  1. Replace the Static HTML data on the home screen with data from the posts table
  2. In the home screen, 0nly display the posts that has been published.

Firstly, we need to modify routes/web.php by adding the following route:

Next, create BlogController :

The newly create BlogController.php will appear inside app/Http/Controllers folder.

Add public function index()

Modify resources/views/blog/index.php

The posts will appear, but still have the same content. We need to do some changes.

Modify Post model and adding accessor public function getImageUrlAttribute :

The accessor will initialise the imageUrl variable, and if the post has image, $imageUrl variable is the image url of the post.

Open Post model and create relationship to user model. Every post has author. We fetch the author from user table :

Open User model and create relationship to post model. One user can create more than one post, and connected with author_id on post table :

Add with method to public function index BlogController, modify BlogController.php

Change the static date on the home screen using carbon diffForHumans method.

Open post model and create date accessor :

Order by the latest post, create scopelatestFirst() in the Post.php model

Open Post model and define the scope, to display the latest post first :

Next, we will create a pagination. We can create it easily using laravel. Modify BlogController.php actionIndex :

Modify resources/views/blog/index.php

Afterwards, we will display only the post that has been published. We need to alter posts table by adding published_at column. Go to the terminal and type :

Open the newly create migration file and modify :

Modify poststableseeder.php and add published_at value :

Migrate and refresh the seed by typing this command inside the terminal :

Create scope published inside Post Model, and modify BlogController to add published posts only :

Modify layouts/main.blade.php to add the route to Home :

To wrap things up, here are the files that we have changed:




The result is as follows :

dynamic data

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.