laravelroute-model-binding

How to find the parameter name expected in laravel model route binding


I have a route like this

Route::get('/vcs-integrations/{vcs-provider}/authenticate','VcsIntegrationsController@authenticate');

and the method to handle the route I am using the model route binding to happen is as follows

<?php

....
use App\Models\VcsProvider;
....

class VcsIntegrationsController extends Controller
{
  public function authenticate(VcsProvider $vcsProvider, Request $request)
    {
        ...
        // some logic
        ...
    }
}

when I try to access the route I am getting 404 due to the parameter name is not matching.

So, how do I know the parameter name expected by laravel in route model binding ?


Solution

  • From the route parameter docs:

    "Route parameters are always encased within {} braces and should consist of alphabetic characters, and may not contain a - character. Instead of using the - character, use an underscore (_)." - Laravel 7.x Docs - Routing - Route Parameters - Required Parameters

    You should be able to define the parameter as {vcs_provider} in the route definition and then use $vcs_provider for the parameter name in the method signature if you would like. You can also name it not using the _ if you prefer, but just avoid the -, hyphen, when naming.

    Have fun and good luck.