How to make a Blog in Laravel

In this article, we will go through how to make a blog in Laravel. We will build a simple blog and admin panel for managing the users and task. Source code for this project is available on GitHub, link: https://github.com/digitalcrm/Blog-Laravel.

1. Installation

First you will need a fresh installation of the Laravel framework. Install the Laravel framework using composer:
“composer create-project –prefer-dist projectname”

2. Prepping the Database

2.1.Connect Database with your Laravel project:

Go to your database server and create database. In this article, I have used the wamp server phpMyAdmin :

Connect with your project, go to “.env” file and change the DB_CONNECTION, Database, User_name and password. In phpMyAdmin the default value of user_name is root and password is null(empty):

3. Make Database Model and Migrations

Next, create migrations and models for your blog. For creating model/migration run the artisan command in your terminal: “php artisan make:model model_name –m”. Here “-m” refers to migrations.

In this article, I have created different models and migrations for User and Admin. The migration will be place in the “database/migrations” and Model will be placed in “app/model” directory of your project.

Model “post.php”

Migration table “create_posts_table.php”

After creating and setup the migrations accordingly to your blog databases, run the migrate command in your terminals. Command: “php artisan migrate”

After running migrate command, tables will be generated in database:

4. Routing

Next, we will add routes to our blog or application. Basically routes are used to points URLs to controllers or closure function that should be executed when a user accesses a given page. By default, all Laravel routes are defined in the “routes/web.php” file.
In this article, I have created many routes for User, Admin, Home and Auth.

5. Controller

After adding the routes, we will need a controller for our blog or project. In Laravel all controller are generated in the “app\Http\Controller” directory. Command:
php artisan make:controller ControllerName –resources
OR
php artisan make:controller ControllerName
Resource Controller command

Resource Controller (“app/Http/Controller/Admin/PostController.php”) file

Controller Command

Controller File (“app/Http/Controller/User/PostController.php”)

In this article, I have created the different controller for the User, Admin and Home part of our blog.

In the Admin controller, I have duplicated the auth controller for admin side authentication purpose.

6. Authentication

Run the auth artisan command in your terminal for creating the authentication in your project.
Command: “php artisan make:auth”.

After running this, the command will generate auth controllers, views and routes for your project.

Controller: Auth controllers are generated in “app/Http/Controllers/Auth” directory.

Views: auth views are generated under in “resources/views/auth” directory.

7. Building Layouts & Views

In this article has multiple views which contain a form for Admin, Blog, role, category, permission, tag, list users and as well as a listing of all blog. As we know all Laravel views are stored in “resources/views”. So we have defined multiple layout view in this directory.

7.1. Layouts
In this layout, we have created the app layouts for Login, Register and Logout.

7.2. Admin Views
In this view, we have created multiple views for admin task and also created an admin panel and blog page layouts.

7.3. User Views
Basically, as we know all web applications share the same layout across pages. So, in this view, we have defined the layouts for users. Laravel makes it easy to share these common features across every page using Blade layouts.

8. Adding Tasks

After creating the view, we have to move for create, update, store, edit, and delete tasks for each route of controller. Example for post-controller:

create

store

edit and update

destroy

9. Authorization

Laravel uses policies to organize authorization logic into simple, small classes. Typically, each policy corresponds to a model. In this article, we create a PostPolicy using the artisan command:
“php artisan make:policy PostPolicy”

After this command, we will add some validation in “post policy” as needed.

10. Blog Interface

Home Page

Admin Dashboard

Blog Create

User’s Blogs