Implementing ID+Slug Routes in Laravel 12 for Article Model
Here's a complete working example of how to implement ID+slug routes for an Article model in Laravel 12:
1. First, create the Article model and migration
php artisan make:model Article -m
2. Update the migration file (database/migrations/xxxx_create_articles_table.php)
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('slug')->unique();
$table->text('content');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('articles');
}
};
3. Update the Article model (app/Models/Article.php)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class Article extends Model
{
use HasFactory;
protected $fillable = ['title', 'slug', 'content'];
protected static function boot()
{
parent::boot();
static::creating(function ($article) {
$article->slug = Str::slug($article->title);
});
}
public function getRouteKeyName()
{
return 'slug';
}
}
4. Create the controller (app/Http/Controllers/ArticleController.php)
<?php
namespace App\Http\Controllers;
use App\Models\Article;
use Illuminate\Http\Request;
class ArticleController extends Controller
{
public function index()
{
$articles = Article::latest()->get();
return view('articles.index', compact('articles'));
}
public function show(Article $article)
{
return view('articles.show', compact('article'));
}
}
5. Set up the routes (routes/web.php)
use App\Http\Controllers\ArticleController;
use Illuminate\Support\Facades\Route;
Route::get('/articles', [ArticleController::class, 'index']);
Route::get('/articles/{article}', [ArticleController::class, 'show'])->name('articles.show');
6. Create the views
resources/views/articles/index.blade.php
@extends('layouts.app')
@section('content')
<h1>Articles</h1>
<ul>
@foreach($articles as $article)
<li>
<a href="{{ route('articles.show', $article) }}">
{{ $article->title }}
</a>
</li>
@endforeach
</ul>
@endsection
resources/views/articles/show.blade.php
@extends('layouts.app')
@section('content')
<h1>{{ $article->title }}</h1>
<p>{{ $article->content }}</p>
<a href="{{ route('articles.index') }}">Back to articles</a>
@endsection
7. Create a seeder to test with (database/seeders/ArticleSeeder.php)
<?php
namespace Database\Seeders;
use App\Models\Article;
use Illuminate\Database\Seeder;
class ArticleSeeder extends Seeder
{
public function run()
{
Article::create([
'title' => 'My First Article',
'content' => 'This is the content of my first article.'
]);
Article::create([
'title' => 'Another Great Article',
'content' => 'This is the content of another great article.'
]);
}
}
8. Run the migrations and seeder
php artisan migrate
php artisan db:seed --class=ArticleSeeder
How It Works
-
The getRouteKeyName() method in the Article model tells Laravel to use the slug field as the route parameter instead of the default id.
-
When you access /articles/122-my-article-title, Laravel will:
- First look for an article with slug "122-my-article-title"
- If not found, it will try to find an article with ID 122 and use its slug
- This provides a fallback mechanism
-
The Str::slug() helper automatically generates a URL-friendly slug from the title when creating articles.
-
The route model binding in the controller automatically resolves the Article model based on the slug.
This implementation gives you the best of both worlds - the SEO benefits of slugs while maintaining the reliability of IDs as a fallback.