Creating a rudimentary, but functional, blog in 14 steps with Laravel 6.4

Lucas Arbex
Nov 6 · 8 min read

Back in 2005, the genius DHH blew everyone’s mind with a video showing how to create a blog in 15 minutes using Ruby on Rails — it was the beginning of the Rails era in web development.

In spite of being a PHP developer, I have to admit, reluctantly, that Rasmus Lerdorf’s language remained several steps behind Ruby for years after the Rails’s advent, and also that several concepts used in PHP frameworks these days are virtually copies from RoR functionalities.

However, regardless of how we got here, nowadays it is safe enough to say that modern PHP is side by side with every back-end programming language used in web development, not only Ruby, but Java, C# and Python as well. I would even say that frameworks such as Laravel, with its huge ecosystem and community, can even take PHP a step up over these other languages.

That being said, and considering that creating a blog is still something ordinary in the web development universe, I will show in this article how to create a rudimentary, but functional, blog in Laravel, with just 14 steps — which can be faster than 15 minutes.

* Note: It is not the purpose of this article to go into details about basic matters, such as installing composer, PHP, Node.js, npm and MySQL (if you need more informations about that, there are thousands of tutorials on the internet showing how to do it). Also, no custom CSS will be shown in this article, since the purpose is just to show the creation of the functionality of a rudimentary blog with Laravel.


Let’s start coding then, always counting our steps!


Step 1)

With composer installed, the first step is to create our project. This can be achieved with a single command:

composer create-project --prefer-dist laravel/laravel blog

Step 2)

Once our structure is mounted, it is time to properly configure the environment file, and point it to the correct database. On our .env file we can configure it as we need. In this case, I will be using localhost, MySQL, and a database called “blog”, like so:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=

Step 3)

It is time to create our post model, which we will call Post, obviously. And also, let’s grasp the opportunity to create our migration with the command -m, to eliminate 1 step in our project.

php artisan make:model Post -m

Step 4)

Now it is time to create our post table structure. We will need the following columns in it; user_id (which will be the post’s author id), title, subtitle and content.

So, let’s just add the correct schema to our recently create migration, which is called create_posts_table.

Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('title');
$table->string('subtitle');
$table->text('content');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});

Step 5)

With the migration properly created, we have to migrate it, of course. Note that when we run the command, the users, password_resets and failed_jobs tables, which came by default with Laravel installation, will also be migrated — which is good for us to eliminate another step in our project.

php artisan migrate

Step 6)

Now, let’s insert a few customizations into our Post model, to add fillable properties to it in order to mass assign them, and let’s also create the relationship between it and the User model.

namespace App;use Illuminate\Database\Eloquent\Model;class Post extends Model
{
protected $fillable = [
'user_id', 'title', 'subtitle', 'content'
];
public function author()
{
return $this->hasOne('App\User', 'id', 'user_id');
}
}

Step 7)

In order to create posts, we will have to have users, of course. So, it’s time to create an user functionality to our application, with registration and login forms, as well as authentication guards and session control. Thanks to Laravel, this task can be achieved in a very simple way, with just a couple of commands.

* Unfortunately, since version 6 of Laravel, you cannot use “php artisan make:auth” anymore.

composer require laravel/ui
php artisan ui vue --auth
npm install
npm run dev

After running these commands, you will be able to see Laravel’s lovely default registration page:

Step 8)

After creating the user functionality, it is time to anticipate what routes we will need, before creating our controller and the form to add new posts to our application.

As authenticated users, we will need two routes, one to show the view with the form to create a post, and one to deal with its request.

As external users, we will also need two routes, one to show every post of our application and one to show a single post, which will accept the id of the post as a parameter.

Let’s create them then.

Route::get('/create-post', 'PostController@create')->name('create');
Route::post('/create-post', 'PostController@store')->name('store');
Route::get('/', 'PostController@index')->name('index');
Route::get('/{post}', 'PostController@show')->name('show');

* Note that I am using {post} in our route because of Laravel’s implicit biding. You can check more information about it in the official documentation.

Step 9)

Finally, it is time to create our controller, which we will customize after creating our form to add new posts. To create it, just run:

php artisan make:controller PostController

Step 10)

Before we get into the controller methods, let’s just create the views we are going to need, without anything inside them at first. To do that, it is necessary to create them by hand in the resources directory. Let’s call them posts, create-post and single, as you can see in the image below.

* “home.blade.php” and “auth” directory were included in our project as a part of the scaffold created on step 7.

Step 11)

This is the final step before we start digging into our controller and really start creating proper back-end functionalities.

We will create the form to add posts in our application. It is as simple as that:

<form action="{{ route('store') }}" method="POST">
@csrf
<input type="hidden" name="user_id" value="{{ Auth::id() }}">
<input type="text" name="title" placeholder="Title"><br>
<input type="text" name="subtitle" placeholder="Subtitle"><br>
<textarea name="content" placeholder="Content"></textarea><br>
<button type="submit">Send</button>
</form>

* I have added a little style customization to my form. If you want to copy it, please check the project on my github.

Step 12)

Finally it is time to create our controller. Firstly, I will show how it has to be, and then I will explain what every method is responsible for.

namespace App\Http\Controllers;use App\Post;class PostController extends Controller
{
public function __construct()
{
$this->middleware('auth')->except('index', 'show');
}
public function index()
{
$posts = Post::paginate(10);
return view('posts', compact('posts'));
}
public function create()
{
return view('create-post');
}
public function store(Post $post)
{
$post->create(request()->all());
return redirect()->route('home');
}
public function show(Post $post)
{
return view('single', compact('post'));
}
}

As you can see, practically nothing is required to create a functional blog.

On the constructor, we instantiate the middleware auth guard onto the controller, with the exception of the methods which do not need to be guarded.

The index and show methods, in turn, simply show posts to the external user, without the necessity of any kind of authentication — one to display a list of all posts with only the title attribute, and one to display all the data of a single post.

Finally, the create method returns the view with the form to create new posts, and the store method deals with the POST request of this form to store the information on the database.

Of course, as the title of this article suggests, this is a rudimentary Blog. There are several other functionalities that you should be doing in order to really create your blog, such as validation, flashing data, redirects and more.

* Note: I have never mentioned TDD, because it would require a lot more steps to create a blog using this technique, and it would have ruined the main idea of the article. However, I strongly recommend to always use TDD, even when developing a small blog like that. Later, I will create a series showing how to do that.

Step 13)

Now, it is time for the last steps in our little project. In the 13th step we will create a simple page to show a list of all of our posts, and distinct links to every single one of them. Remember that in our aforementioned controller we have created an index method, which is passing a Collection of posts (under the variable $posts) to our view. So, we can simply use it on a foreach loop, and then link it to the route named show, passing the id of each post as a parameter.

@foreach ($posts as $post)
<h1>
<a href="{{ route('show', $post->id) }}">
{{ $post->title }}
</a>
</h1>
@endforeach

Step 14)

Finally, our last step!

The last thing we have to do is creating the single post’s view, which we have returned on our show method of our controller, with the variable $post as a parameter.

In there you can customize however you want, using the Post object. For instance:

<h1>{{ $post->title }}</h1>
<h4>{{ $post->subtitle }}</h4>
<h6>
Author: <strong>{{ $post->author->name }}</strong> |
Date: {{ $post->created_at->format('Y-m-d') }}
</h6>
<hr>
<p>{{ $post->content }}</p>

That is it! With only these simple 14 steps you have a functional blog in Laravel 6.4!

As I mentioned, though, this is a rudimentary blog, with just the most basic stuff. However, from here, you can dig deeper on your own, create validations, redirects, flash messages, custom style, and more. When you really learn Laravel, creating these simple web applications becomes as easy as riding a bike.

But remember, always use TDD. TDD is the way to go!

Lucas Arbex

Written by

Passionate about technology, adept to free software and opensource projects.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade