Stephane
Stephane

Reputation: 5078

Failed Laravel dependency injection in controller method

I have a model called Dbtable which isn't injected when used like this:

public function showEditDbTableForm(Request $request, DbTable $table) 
{

}

it only works when I do this:

public function showEditDbTableForm(Request $request, $id)
{
    $table = DbTable::find( $id );
}

Same thing happens even when I rename DbTable to DbTble

P.S.: please don't be rude with me as I'm new to Laravel framework

Upvotes: 1

Views: 617

Answers (2)

Davit Zeynalyan
Davit Zeynalyan

Reputation: 8618

In RouteServiceProvider class add

public function boot()
{
    parent::boot();

    Route::model('db-table', App\DbTable::class);
    // db-table correspond your rout parameter
}

see official documentation https://laravel.com/docs/5.5/routing Explicit Binding section

Upvotes: 1

lagbox
lagbox

Reputation: 50491

For Implicit Route Model Binding you need to make sure the parameter in the method signature has the same name as the route parameter you want to bind.

Route::get('widgets/{widget}', 'WidgetsController@show');

public function show(Widget $widget)

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

Laravel 5.6 Docs - Routing - Implicit Model Binding

Upvotes: 2

Related Questions