Shobi
Shobi

Reputation: 11461

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 ?

Upvotes: 0

Views: 1174

Answers (2)

lagbox
lagbox

Reputation: 50491

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.

Upvotes: 3

Ziad Adeela
Ziad Adeela

Reputation: 496

Laravel automatically resolves Eloquent models defined in routes or controller actions whose type-hinted variable names match a route segment name.

This means that if you want implicit binding to work, you need to name the route param same as your variable. Since your variable's name is vcsProvider, your route should be:

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

Upvotes: 1

Related Questions