3

Having trouble with Model Bind on davejamesmiller/laravel-breadcrumbs. I'll try to be brief, but if you need more data just ask =D This is my controller/action signature:

public function edit(Request $request, Vertical $vertical, UserMacro $macro)

And this is my BC for the corresponding route:

Breadcrumbs::register('macro-edit', function (Generator $breadcrumbs, $vertical, $macro) {
    $breadcrumbs->parent('macro-index');
    $breadcrumbs->push($macro->name, route('macro-edit', [$vertical->_id, $macro]));
});

I'm getting the string ID on $vertical and $macro, breaking on $macro->name. If I add Hints as the action have I get aType error.

Trying to get property of non-object (View: /.../resources/views/layouts/app.blade.php) (View: /.../resources/views/layouts/app.blade.php)


Type error: Argument 2 passed to DaveJamesMiller\Breadcrumbs\BreadcrumbsServiceProvider::{closure}() must be an instance of App\Vertical, string given, called in /.../vendor/davejamesmiller/laravel-breadcrumbs/src/BreadcrumbsGenerator.php on line 68 (View: /.../resources/views/layouts/app.blade.php) (View: /.../resources/views/layouts/app.blade.php)
CesarScur
  • 84
  • 7
  • have you set the `$primaryKey` attribute on your model, and set `$incrementing` to false? Not doing so might prevent Laravel from resolving the string to a `Vertical` – Jeff Jan 31 '18 at 14:00
  • The action is presenting a model correctly loaded. My problem is only with breadcrumbs {closure} receiving a string (id) – CesarScur Jan 31 '18 at 16:32

1 Answers1

0

I didn't analyze core code of library, so i don't know why controllers work, but breadcrumbs don't. Recipe to working model binding is use proper route naming convention.

Breadcrumbs::for('messages.show', function($trail, \App\Models\MassMessage $massMessage) {
    $trail->parent('Index');
    $trail->push('Show', route('messages.show', $massMessage));
});

Route::get('messages/{massMessage}', 'MessageController@show')->name('messages.show');
// error (controllers are fine)


Route::get('mass-mmessages/{massMessage}', 'MessageController@show')->name('messages.show');
 // works both

The same problem is with resource route.

edit:

I was wrong. Controller also doesn't work. It not raise error but it pass empty Eloquent object. In my case i needed to change $massMessage variable's name into $message in both places and works fine, now.