Saturday, March 17, 2018

Laravel 5.6 Route Model Binding Behaving Weirdly - development

Laravel 5.6 Route Model Binding Behaving Weirdly

This is a really strange problem because I’ve never had any issues with route model binding before. I have a resource route:

Route::resource('metadata','MetadataController');

I have a redirect in the index method of my MetadataController that gets fired when someone clicks on a link.

return redirect()->route('metadata.edit', 1);

In my RouteServiceProvider.php file I have the following:

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

    Route::model('metadata', AppModelsMetadata::class);
    Route::model('settings', AppModelsSettings::class);
}

Note that I have an identical setup for my settings Route/Controller and it works fine.

In my edit method I have the following:

public function edit(Metadata $metadata)
{
    dd($metadata); // Never reaches here
}

Can someone explain to me why this route isn’t binding properly? (or at all)

Additional notes:

If I remove the route binding from the RouteServiceProvider and the type casting to Metadata in the edit method, it does call the edit method.

Both of these lines work just fine:

 Route::model('settings', AppModelsSettings::class);
 Route::model('settings', AppModelsSettings::class);

This line doesn’t throw an error but gives me a page not found message:

 Route::model('metadata', AppModelsMetadata::class);

This line throws an error:

 Route::model('metadata', AppModelsMetadata::class);

Class AppProvidersAppModelsMetadata does not exist

Seems like there’s a bit of weirdness going on and I am not sure what it is, thoughts?



from Laravel Questions and Answers https://laravelquestions.com/php/laravel-5-6-route-model-binding-behaving-weirdly/
via Lzo Media

No comments:

Post a Comment