diglactic / laravel-breadcrumbs

Laravel Breadcrumbs - A simple Laravel-style way to create breadcrumbs.
https://packagist.org/packages/diglactic/laravel-breadcrumbs
MIT License
868 stars 63 forks source link

Error when trying to use automatic model binding #29

Closed bitserve-jelle closed 3 years ago

bitserve-jelle commented 3 years ago

When` I try to do this (SpecificModel can be any type):

Breadcrumbs::for('specificRoute', function (BreadcrumbTrail $trail, SpecificModel $modelInstance) {
    $trail->push($modelInstance->name, route('specificRoute', $modelInstance));
});

I get this error:

TypeError Diglactic\Breadcrumbs\ServiceProvider::{closure}(): Argument #2 ($modelInstance) must be of type SpecificModel, array given, called in ***/vendor/diglactic/laravel-breadcrumbs/src/Generator.php on line 64

Could anyone tell me how to fix this? This template is explicitly given as an option in the README if I am not mistaken.

shengslogar commented 3 years ago

Can you remove the SpecificModel typing and dd($modelInstance)? It sounds like this exception is saying $modelInstance is coming in as an array which means route-model binding might not be working at the controller level.

If dumping $modelInstance indeed resolves to a model, then this a problem with the package.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

bitserve-jelle commented 3 years ago

Hi, excuse me for my response being late. I just saw your response. I think it was an issue due to Livewire. For this the SubstituteBinding middleware might not be able to determine the proper Model instances due to a different structure. Livewire itself seems to be able to do the substitution, but that one came later than my method call.

shengslogar commented 3 years ago

Hm, I haven't used Livewire myself, so I'm not much help here. This might be related? https://github.com/livewire/livewire/issues/2558 It doesn't seem to line up with your original stack trace, but you're more than welcome to:

Breadcrumbs::for('specificRoute', function (BreadcrumbTrail $trail, string $modelKey) {
    $resolvedModel = SomeModel::findOrFail($modelKey);
});

if needed.

If you end up opening an issue with Livewire, please link that here.