1

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

8
  • what do you mean by "it isn't injected", is there an error? But if you are talking about 'route model binding' the route parameter name has to match the method parameter name Commented Jun 2, 2018 at 18:56
  • The second parameter of the showEditDbTableForm is the id for a DbTable record. So, when using DbTable $table I expect the model to be retrieve automatically Commented Jun 2, 2018 at 19:00
  • What error do you get if you do it in the first way? Commented Jun 2, 2018 at 19:01
  • reread the 'route model binding' documentation, the route parameter name must match the method parameter name (for implicit binding), might be some other goodies in there too :) but guessing since i can't see a route definition Commented Jun 2, 2018 at 19:01
  • @lagbox Ok I see what you mean Commented Jun 2, 2018 at 19:03

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.