Mahmoud Al-HabashOpen to new projects
  • WorkWork
  • ServicesServices
  • LabLab
  • WritingWriting
  • ContactContact
Amman--:--عربي
Writing/Laravel

Laravel Route Model Binding: Cleaner Controllers

Automatically inject model instances into your route actions.

16 January 20261 min readLaravelاقرأ بالعربية

On this page

  1. Implicit Binding
  2. Custom Key
  3. In Model
  4. Scoped Binding

Cleaner routes with model binding:

Implicit Binding

php
Route::get('/posts/{post}', function (Post $post) {
    return $post;
});

Custom Key

php
Route::get('/posts/{post:slug}', function (Post $post) {
    return $post;
});

In Model

public function getRouteKeyName()
{
    return 'slug';
}

Scoped Binding

php
Route::get('/users/{user}/posts/{post:slug}', function (User $user, Post $post) {
    return $post;
})->scopeBindings();
All articles

Keep reading

  • Laravel Action Classes Pattern16 January 20261 min read
  • Laravel Action Classes: Single Responsibility16 January 20261 min read
  • Laravel API Rate Limiting16 January 20261 min read