Creating a Blog with SEO-Ready URLs in Laravel

by Pragati S. 4 minute read 11 views

More than 65% of Laravel projects incorporate SEO features, and 80% of developers favour slugs for improved website traffic and user engagement.

Key Points

  • Studies show 75% of Laravel apps utilise slugs to enhance readability and maintain tidy URL structures.
  • About 68% of developers say SEO slugs help lower bounce rates and boost page views.
  • Surveys reveal that 82% of new Laravel sites prioritise SEO-friendly routes and sluggable URLs today.

Overview

Clean, SEO-friendly URLs are essential for any modern website. Whether you manage a blog or an enterprise platform, SEO slugs help boost ranking and build user trust. For businesses committed to improving search visibility, it’s wise to hire SEO experts and collaborate with a reliable web development company.

In this guide, you’ll learn how to create a simple blog in Laravel with SEO-friendly URLs using the eloquent-sluggable package. This is a common method among professionals offering website development services and custom solutions.

Let’s get started!

Step 1: Install Package

We have to add the eloquent-sluggable package to generate unique slug URLs.

Run:

                                        composer require cviebrock/eloquent-sluggable
                                    

After successfully installing the package, open your config/app.php file and add the service provider and alias:

                                         'providers' => ServiceProvider::defaultProviders()->merge([
        /*
         * Package Service Providers...
         */


        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,
        Cviebrock\EloquentSluggable\ServiceProvider::class,




    ])->toArray(),
                                    

You can publish the default configuration file with the following command:

                                        php artisan vendor:publish --provider="Cviebrock\EloquentSluggable\ServiceProvider"
                                    

Implementing slugs is among the top practices suggested by experts who hire Laravel developers for SEO-friendly applications.

Step 2: Create Blog Table and Model

In this step, we have to create a migration for the blogs table using the php artisan command:

                                        php artisan make:migration create_blogs_table
                                    

After running this command, you’ll find one migration file in:

                                        database/migrations
                                    

Add the following code to your migration file to create the blogs table:

                                        <?php


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;


return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('blogs', function (Blueprint $table) {
           $table->increments('id');
           $table->string('title');
           $table->string('slug');
           $table->timestamps();
        });
    }


    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('blogs');
    }
};
                                    

After creating the blogs table, you should create the Blog model. Create a new file:

                                        app/Models/blogs.php
                                    

And add the following code:

                                        <?php


namespace App\Models;


use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;


use Cviebrock\EloquentSluggable\Sluggable;


class blogs extends Model
{
    use Sluggable;






    public $fillable = ['title'];


    /**
     * Return the sluggable configuration array for this model.
     *
     * @return array


     */


    public function sluggable(): array
    {
        return [
            'slug' => [
                'source' => 'title'
            ]
        ];
    }
}
                                    

This setup ensures that whenever a blog post is created, the slug field is automatically generated from the title. Such practices are essential in professional website development for creating SEO-friendly URLs.

Step 3: Create Route

Add these routes in routes/web.php to handle the blog functionality:

                                        Route::get('/blog', [BlogPostController::class, 'index'])->name('blog.index');
Route::get('/blog/list', [BlogPostController::class, 'list'])->name('blog.list');
Route::post('/blog', [BlogPostController::class, 'store'])->name('blog.store');
Route::get('/blog/{slug}',[BlogPostController::class, 'show'])->name('blog.show');
                                    

Such routing structures are frequently used in custom web development to keep URLs SEO-friendly and intuitive.

Step 4: Create Controller

Create your controller file:

                                        app/Http/Controllers/BlogPostController.php
                                    

Add the following content:

                                        <?php


namespace App\Http\Controllers;


use App\Models\blogs;
use Illuminate\Http\Request;




class BlogPostController extends Controller
{


    public function index()
    {
        return view('blog.create');
    }


    public function store(Request $request)


    {
        $this->validate($request, ['title' => 'required']);
        $items = blogs::create($request->all());
        return back();
    }


    public function list()
    {
        $data['items'] = blogs::get();
        return view('blog.index',  $data);
    }


    public function show($slug)
    {
        echo "Single blog view";
    }
}
                                    

This controller handles listing, creating new blog entries, and displaying individual blog posts by their slug. Many companies hire Laravel developers specifically for building such elegant, SEO-focused structures.

Step 5: Create View Blog Post Form

Create a view file at:

                                        resources/views/blog/create.blade.php
                                    

Use the exact code below for the blog creation form:

                                        @extends('layouts.app') {{-- or your base layout --}}


@section('content')
<div class="container mt-5">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <h2 class="mb-4 text-center">Create Blog Post</h2>


            @if ($errors->any())
                <div class="alert alert-danger">
                    <ul class="mb-0">
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif


            <div class="shadow-sm card">
                <div class="card-body">
                    <form action="{{ route('blog.store') }}" method="POST">
                        @csrf


                        <div class="mb-3">
                            <label for="title" class="form-label">Title</label>
                            <input type="text" name="title" id="title" class="form-control" value="{{ old('title') }}" required>
                        </div>


                        <div class="mb-3">
                            <label for="content" class="form-label">Content</label>
                            <textarea name="content" id="content" class="form-control" rows="6" required>{{ old('content') }}</textarea>
                        </div>


                        <div class="text-end">
                            <button type="submit" class="btn btn-primary">Create Post</button>
                        </div>
                    </form>
                </div>
            </div>


        </div>
    </div>
</div>
@endsection
                                    

This form helps you to create new blog posts with SEO-friendly slugs—an important feature for any modern web development business.

Step 6: Create View Listing

Create a listing view at:

                                        resources/views/blog/index.blade.php
                                    

Use this code exactly as provided:

                                        @extends('layouts.app') {{-- or your base layout --}}


@section('content')
    <h3>Blogs</h3>
    <div class="panel panel-primary">


        <div class="panel-heading">Blogs</div>


        <div class="panel-body">


            <table class="table table-bordered">


                <thead>


                    <th>Id</th>


                    <th>Title</th>


                    <th>URL</th>


                    <th>Creation Date</th>


                    <th>Updated Date</th>


                </thead>


                <tbody>


                    @if ($items->count())
                        @foreach ($items as $key => $item)
                            <tr>
                                <td>{{ ++$key }}</td>
                                <td>{{ $item->title }}</td>
                                <td><a href="{{ URL::to('/') . '/blog/' . $item->slug }}">{{ URL::to('/') . '/blog/' . $item->slug }}</a></td>
                                <td>{{ $item->created_at }}</td>
                                <td>{{ $item->updated_at }}</td>
                            </tr>
                        @endforeach
                    @else
                        <tr>
                            <td colspan="4">There are no data.</td>
                        </tr>
                    @endif


                </tbody>


            </table>
        </div>
    </div>
    </div>
@endsection
                                    

This view produces a table of blog posts, displaying their SEO-friendly URLs. Many businesses focus on this to improve search rankings and user experience, often hiring website development services.

Output

Your Laravel blog is now ready!

  • Access the blog form at:
    /blog
  • See a listing of blog posts at:
    /blog/list
  • Visit each blog post with SEO-friendly URLs like:
    https://yourdomain.com/blog/my-first-blog-post

Final Words

Creating SEO-friendly URLs in Laravel is straightforward with the eloquent-sluggable package. Clear slugs enhance your SEO, readability, and brand image. Whether you’re an entrepreneur or managing a large platform, it’s wise to collaborate with professionals who hire SEO experts and experienced developers for customised web development.

If you’re planning a project, consider engaging a reputable web development company to bring your ideas to life, and ensure your SEO stays effective!

Tech Stack & Version

Frontend

  • HTML5
  • CSS3
  • Bootstrap

Backend

  • PHP ≥ 7.2
  • Laravel ≥ 6.0
  • MySQL

Deployment

  • AWS EC2
  • DigitalOcean
  • Linode

img

©2025Digittrix Infotech Private Limited , All rights reserved.