glhd / gretel

Laravel breadcrumbs right out of a fairy tale
MIT License
243 stars 16 forks source link

Add support for shallow nested resources #9

Open dsazup opened 2 years ago

dsazup commented 2 years ago

Hey! Just tried out this package and it seems great, but I run into a problem where shallow nested resources are not working correctly. I tried to fix this with limited amount of time and knowledge about this package, but seems like I got the tests to pass, so let me know if you think this is ok.

Basically if we have something like this (same as in the test) and we visit notes/1/edit, then it wouldn't display anything at all, because in the registry it's registered as users.notes.edit and not notes.edit, but the route when it is resolved has name of notes.edit. So this change basically makes sure it is registered with shallow name.

Route::resource('users', ResourceRoutesTestController::class)
    ->breadcrumbs([
        'index' => 'Users',
        'create' => 'New User',
        'edit' => 'Edit',
    ]);

  Route::resource('users.notes', NotesController::class)
    ->shallow()
    ->breadcrumbs(fn(ResourceBreadcrumbs $breadcrumbs) => $breadcrumbs
        ->show(fn(Note $note) => $note->note, 'users.index', fn(Note $note) => $note->user)
        ->edit('Edit', '.show', fn(Note $note) => $note->user)
    );

Let me know what you think 👍