ESC

Search on this blog

Weekly updates

Join our newsletter!

Do not worry we don't spam!

A Laravel 10 Basic Blog System Tutorial


Building your own custom blog from scratch is one of the best ways to apply your Laravel skills to bring an idea to life. In this tutorial, let’s create a blogging platform named "BlogCastle" step-by-step using Laravel 10 features like routing, controllers, databases, models, relationships and views.

Outlining BlogCastle Features

Before diving into code, let’s broadly outline the capabilities needed:

  • - View all blog posts on homepage
  • - CRUD blog posts 
  • - Markdown editor for writing posts  
  • - Basic search 
  • - Author profiles
  • - Post categories
  • - Commenting system
  • - User authentication 
  • - Admin panel

We will progressively work through making these features functional. The goal is to end up with a simple yet complete custom blogging system by the end.

 

Initial Setup

Building BlogCastle requires a standard Laravel installation on your development machine. Follow [the docs](https://laravel.com/docs/10.x) to get latest version up and running.

With basic Laravel scaffolding generated, customize default homepage route in `routes/web.php`:


Route::get('/', function () {
   return view('homepage');
});

We will progressively flesh out the `homepage` view and other templates shortly.

 

Database Structure

Naturally, blogs center around textual content — blog posts and articles. Users can view, search and comment on these posts.  

So our main database entities include:

- Users - Registered bloggers and readers 
- Posts - Actual blog content entries 
- Comments - Reader reactions and discussions
- Categories- Topics to organize blog posts  

We will use Laravel migrations to create corresponding database tables incrementally as needed.

 

Basic CRUD Routes & Models

With the blueprint in place, let’s start bringing BlogCastle to life! We will take an outside-in approach focusing on routes and controllers first before building underlying models.

Generate basic CRUD routes for posts:


php artisan make:controller PostController --resource

This creates RESTful routes for posts resource:


+--------+-------------------------+-------------------------+--------------------+--------------------------------------------------+--------------+
| Domain | URI                     | Name                    | Action             | Middleware                                       | Name         |
+--------+-------------------------+-------------------------+--------------------+--------------------------------------------------+--------------+ 
|        | GET|HEAD   posts         | posts.index             | App\Http\Controllers\PostController@index        | web          |
|        | POST      posts         | posts.store             | App\Http\Controllers\PostController@store        | web          |

Also generates skeleton controller methods for respective routes.

With routes laid out, build Post model extending Eloquent:

bash
php artisan make:model Post

 


class Post extends Model
{
 // Table name
 protected $table = 'posts'; 
}

Run migrations to create posts table:

bash 
php artisan make:migration create_posts_table

 


Schema::create('posts', function (Blueprint $table) {
   $table->id();
   $table->string('title');
   $table->text('body');
   $table->timestamps();
});

Models connect to associated database tables simplifying CRUD operations as we will see shortly.


READ ALSO:


Displaying Blog Posts

With the backend pieces wired up, let’s focus on presenting posts on the front end. Start displaying existing posts on our home page:


public function index()
{
 $posts = Post::simplePaginate(5);
 
 return view('homepage', ['posts' => $posts]); 
}
```

Update `homepage.blade.php` view to display posts:


@extends('layout')
<!-- Display posts -->
@foreach ($posts as $post)
 <article>
   <h1>{{ $post->title }}</h1>
   <div>{{ $post->body }}</div>
 </article>
@endforeach
{{ $posts->links() }}

Posts will automatically render via Eloquent model binding!

 

Creating New Posts

Let registered users create new blog posts to populate content.

Build create form:

//blade
<!-- Create Post Form -->
<form method="POST" action="/posts">

 @csrf
 <input name="title"/>
 <textarea name="body"></textarea>
 <button>Publish</button>
</form>

Handle submission in PostController:


public function store(Request $request) 
{
 $post = Post::create([
   'title' => $request->title,
   'body' => $request->body
 ]);
 return redirect($post->path());
}
```

That’s it! The post gets persisted to the database automatically.

 

Updating Posts

Modify existing posts using similar patterns:

blade
<form method="POST" action="/posts/{{ $post->id }}">
 @csrf
 @method('PUT')
 ...
 <button>Update</button>
 
</form>

Process form in controller:


public function update(Request $request, Post $post)
{  
   $post->update([
    // Populate attributes 
   ]);
   return redirect($post->path());  
}

Laravel assigns given post model instance automatically making updates a breeze!

And that’s it! By stitching together routes, controllers, models and views — we now have a simple yet functional custom blog engine.

 

Fleshing Out Remaining Features

Let’s brisk through implementations for remaining features:

Authentication
Scaffold boilerplate auth using:


php artisan make:auth

Restrict creating posts to logged in users.

 

Markdown Editor
Add JavaScript markdown editor like SimpleMDE to enhance writing. Store generated HTML.

 Categories
Create and assign categories to categorize posts:


php artisan make:model Category -m

Attach to posts via a pivot table.

Author Profiles
Associate posts to logged in users automatically. Show author details on posts.


READ ALSO:


Comments  
Scaffold comments table and model with relationships to posts and users.


php artisan make:model Comment -m

Display on post view using nested relationships.  

Search
Build search screen with basic keyword search for titles and bodies.

Admin Panel
Add admin user role via middleware. Build /admin section for data management.

And we will have a simple yet complete blog system with all common features using the Laravel framework!

Final Touches

Some finishing touches:

- Add editorconfigs for coding style consistency
- Email notifications for comments
- RSS feeds 
- Analytics integration  
- API support
- Advanced media management
- Package up admin panel  

Possibilities are endless for further enhancements!

Conclusion

I hope you enjoyed this introductory walkthrough on building a custom blogging platform with Laravel step-by-step. Of course production systems get far more complex with scalability, security and optimization. But you now have broad exposure to foundational patterns and best practices for web development.

What features would you like to see added to BlogCastle? Would love to hear your suggestions and feedback!

How to Host and Deploy a React App with Firebase
Prev Article
How to Host and Deploy a React App with Firebase
Next Article
JavaScript in 2024: Why It's Still the King of the Web (and Beyond)
JavaScript in 2024: Why It's Still the King of the Web (and Beyond)

Related to this topic: