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.

1// Route
2Route::get('{product:slug}', Controller::class);
3 
4// Request
5https://nodejs.pub/@unlock/hello-world
6 
7// Product Model
8public function resolveRouteBinding($value, $field = null)
9{
10 $value = str_replace('@', '', $value);
11 
12 return parent::resolveRouteBinding($value, $field);
13}

Tip given by @Philo01

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord