DEV Community

Cover image for Laravel 9 - Override the route binding resolver for each of your models
Sandro Jhuliano Cagara
Sandro Jhuliano Cagara

Posted on

2

Laravel 9 - Override the route binding resolver for each of your models

You can override the route binding resolver for each of your models.

In this example, I have no control over the @ sign in the URL, so using the resolveRouteBinding method, I'm able to remove the @ sign and resolve the model.

// Route
Route::get('{examples:slug}', ExampleController::class);

// Request
https://nodejs.pub/@unlock/hello-world

// Example Model
public function resolveRouteBinding($value, $field = NULL)
{
    $value = str_replace('@', '', $value);
    return parent::resolveRouteBinding($value, $field);
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more