Table of Contents
What is Route Model Binding in Laravel?
Implicit Route Model Binding
id
. Laravel automatically resolves the route parameter to a corresponding model.
Example:
If you have a route like this:
Route::get('/users/{user}', [UserController::class, 'show']);
class UserController extends Controller
{
public function show(User $user)
{
return view('users.show', compact('user'));
}
}
When a user visits /users/1, Laravel will automatically fetch the user with id 1 from the database and pass the User model instance to the show method.
Explicit Route Model Binding
slug
.
Example:
In theRouteServiceProvider
:
use App\Models\Post;
public function boot()
{
Route::model('post', Post::class);
}
Now, you can define a route with the {post}
parameter, and Laravel will automatically fetch the Post
model based on the default key or a custom key like a slug if defined in the model.
Why Use Slug for Route Binding?
A slug is a URL-friendly version of a string, typically used for SEO-friendly URLs. For example, instead of /posts/12
, you can have /posts/laravel-tutorial
where laravel-tutorial
is a slug. It’s more readable and better for SEO.
Setting Up Route Model Binding with Slug
Step 1: Add a Slug Field to Your Model
php artisan make:migration add_slug_to_posts_table --table=posts
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->string('slug')->unique();
});
}
php artisan migrate
Step 2: Update Your Model
Update your model (e.g., Post
) to include the slug. Make sure your model has a method to fetch records by slug instead of id
.
class Post extends Model
{
protected $fillable = ['title', 'content', 'slug'];
// Find route model binding by slug instead of id
public function getRouteKeyName()
{
return 'slug';
}
}
The getRouteKeyName
method tells Laravel to use the slug
field for route model binding.
Step 3: Define the Route
Route::get('/posts/{post}', [PostController::class, 'show']);
Step 4: Implement the Controller
In your controller, you can directly use the Post
model instance passed to the show
method:
class PostController extends Controller
{
public function show(Post $post)
{
return view('posts.show', compact('post'));
}
}
With this setup, when you visit /posts/laravel-tutorial
, Laravel will automatically fetch the Post
model where the slug
is laravel-tutorial
.
Step 5: Generate Slugs Automatically
You can use Laravel’s string helper functions to generate slugs automatically. For instance, when creating or updating a post, you can generate the slug based on the title:
use Illuminate\Support\Str;
class Post extends Model
{
protected static function boot()
{
parent::boot();
static::saving(function ($post) {
$post->slug = Str::slug($post->title);
});
}
}
Benefits of Using Slug in Route Model Binding
- SEO-Friendly URLs: Using slugs improves the readability and SEO of your URLs.
- Automatic Model Resolution: Laravel’s route model binding saves you from writing manual queries.
- Unique Identification: Slugs act as a unique identifier, making URLs more meaningful to users.
Final Thoughts
Implementing Laravel route model binding with slug is a powerful and developer-friendly approach to enhancing your application’s URLs. With just a few tweaks in your model, routes, and controller, you can have more readable, SEO-optimized URLs while leveraging the power of Laravel’s automatic route model binding.
By following this guide, you can efficiently use slugs in your Laravel applications, providing both a better user experience and improved SEO.