Naar de inhoud
Recognized by Laravel Match je project Contact

Gebruik route model binding

Voor het laatst bijgewerkt op:

Introductie

Laravels impliciete route model binding resolveert Eloquent-modellen automatisch vanuit route-parameters, waardoor handmatige findOrFail()-aanroepen overbodig worden. In combinatie met scoped bindings voor geneste resources en resource controllers houdt dit routingcode beknopt, consistent en minder foutgevoelig.

Waarom

  • Minder boilerplate: Geen handmatige findOrFail()- of find()-aanroepen nodig, Laravel resolveert het model automatisch
  • Automatische 404-afhandeling: Als het model niet gevonden wordt, geeft Laravel een 404-response terug zonder extra code
  • Afdwingen van parent-child-relaties: Scoped bindings zorgen ervoor dat geneste resources daadwerkelijk bij hun parent horen, wat ongeautoriseerde toegang voorkomt
  • RESTful consistentie: Resource controllers dwingen standaard CRUD-naamgevingsconventies af in de hele applicatie

Geschikt voor

  • Elke route die op een specifieke model-instantie werkt
  • Geneste resource-routes (bijv. /users/{user}/posts/{post})
  • RESTful API's en CRUD-controllers
  • Applicaties waar consistente URL-patronen de developer experience verbeteren

Minder geschikt voor

  • Routes die aangepaste resolutielogica nodig hebben die verder gaat dan eenvoudige key-lookups
  • Endpoints die niet op specifieke model-instanties werken
  • Legacy-routes met niet-standaard parameternaamgeving

Voorbeelden

Impliciete route model binding

// Slecht: handmatige resolutie
public function show(int $id)
{
    $post = Post::findOrFail($id);
}

// Goed: automatische resolutie met type-hinting
public function show(Post $post)
{
    return view('posts.show', ['post' => $post]);
}

Scoped bindings voor geneste resources

Dwing parent-child-relaties automatisch af:

Route::get('/users/{user}/posts/{post}', function (User $user, Post $post) {
    // $post wordt automatisch gescoped op $user
})->scopeBindings();

Gebruik resource controllers

Route::resource('posts', PostController::class);
Route::apiResource('api/posts', Api\PostController::class);

Meer info

Skill

Laravel Boost Skill

Gebruik deze skill om de richtlijn rechtstreeks toe te passen met een AI-assistent.

Use this skill when a Laravel task touches this best practice. It is self-contained so it can be installed independently by Laravel Boost or another agent-skill system.

## Core Guidance

Laravel's implicit route model binding automatically resolves Eloquent models from route parameters, eliminating manual `findOrFail()` calls. Combined with scoped bindings for nested resources and resource controllers, this keeps routing code concise, consistent, and less error-prone.

## Why It Matters

- **Less boilerplate**: No need for manual `findOrFail()` or `find()` calls, Laravel resolves the model automatically
- **Automatic 404 handling**: If the model isn't found, Laravel returns a 404 response without any extra code
- **Parent-child enforcement**: Scoped bindings ensure nested resources actually belong to their parent, preventing unauthorized access
- **RESTful consistency**: Resource controllers enforce standard CRUD naming conventions across the application

## Apply When

- Any route that operates on a specific model instance
- Nested resource routes (e.g., `/users/{user}/posts/{post}`)
- RESTful APIs and CRUD controllers
- Applications where consistent URL patterns improve developer experience

## Be Careful When

- Routes that need custom resolution logic beyond simple key lookups
- Endpoints that don't operate on specific model instances
- Legacy routes with non-standard parameter naming

## Canonical Source

- Full best practice: https://github.com/Dutch-Laravel-Foundation/best-practices/blob/main/routing/use-route-model-binding/BEST_PRACTICE.md
- Dutch translation: https://github.com/Dutch-Laravel-Foundation/best-practices/blob/main/routing/use-route-model-binding/translations/nl.md

## Workflow

1. Inspect the user's Laravel code before recommending changes.
2. Identify the narrow rule from this best practice that applies to the task.
3. Prefer Laravel's built-in conventions and documented APIs over custom abstractions.
4. Keep examples focused on this practice; reference other skills or practices when the task crosses boundaries.
5. Verify code changes with the project's available tests, linters, static analysis, or framework checks.

## Review Checklist

- The recommendation is Laravel-specific and grounded in this practice.
- Code examples use realistic Laravel file names, class names, and method names.
- The advice avoids mixing unrelated architecture, deployment, security, or testing topics.
- Related practices are mentioned when useful, but not re-explained in full.
- Dutch output, when requested, keeps framework and API names intact.