glhd / gretel

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

"Missing required parameter for Route" error with resource routing #18

Open cowgod opened 11 months ago

cowgod commented 11 months ago

I'm not sure if this is a bug or just lack of understanding on my part, as I'm fairly new to Laravel. I'm trying to implement this package in the Laravel app I'm building, but I'm running into an issue getting resource routing to work, in a case where the parent for the show route for one (nested) resource needs to be the show route for its parent resource. I can't find a way to define the breadcrumb for the child show route that loads without error.

What version does this affect?

To Reproduce

Here is the relevant excerpt from the routes/web.php file:

    Route::get('/', function () {
        return view('home');
    })
         ->name('home')
         ->breadcrumb('Home')
    ;

    Route::resource('repositories', RepositoryController::class)
         ->only(['index', 'show'])
         ->breadcrumbs(function (ResourceBreadcrumbs $breadcrumbs) {
             $breadcrumbs
                 ->index('Repositories')
                 ->show(fn(Repository $repository) => $repository->name)
             ;
         })
    ;

    Route::resource('repositories.repository_accounts', RepositoryAccountController::class)
         ->only(['index', 'show'])
         ->breadcrumbs(function (ResourceBreadcrumbs $breadcrumbs) {
             $breadcrumbs
                 ->index('Accounts')
                 ->show(fn(RepositoryAccount $ra) => $ra->identifier)
//               ->show(fn(RepositoryAccount $ra) => $ra->identifier, 'repositories.show')
//               ->show(fn(RepositoryAccount $ra) => $ra->identifier, 'repositories.show', fn(RepositoryAccount $ra) => $ra->repository)
             ;
         })
    ;

As you can see, I've tried three different alternatives of the show() function for the child resource. Neither works, but they give different errors when trying to access the child resource's show route:


show(fn(RepositoryAccount $ra) => $ra->identifier)

Error: Missing required parameter for [Route: repositories.repository_accounts.index] [URI: repositories/{repository}/repository_accounts] [Missing parameter: repository].


show(fn(RepositoryAccount $ra) => $ra->identifier, 'repositories.show')

Error: Illuminate\Routing\RouteFileRegistrar::{closure}(): Argument #1 ($ra) must be of type App\Models\RepositoryAccount, App\Models\Repository given, called in /app/vendor/glhd/gretel/src/Resolvers/Resolver.php on line 35


show(fn(RepositoryAccount $ra) => $ra->identifier, 'repositories.show', fn(RepositoryAccount $ra) => $ra->repository)

Error: Illuminate\Routing\RouteFileRegistrar::{closure}(): Argument #1 ($ra) must be of type App\Models\RepositoryAccount, App\Models\Repository given, called in /app/vendor/glhd/gretel/src/Resolvers/Resolver.php on line 35


Expected behavior The child's show route loads successfully.

Additional context

It may be relevant that I'm also using slugs for my models' route keys, using Spatie's laravel-sluggable package, so the route for the show endpoint on my child resource resource looks like /repositories/active-directory/repository_accounts/abigailbruen. However, I've tested disabling the getRouteKey() definitions in both the parent and child model, which results in my route looking like /repositories/1/repository_accounts/9, and the problem still occurs.

just-tom commented 7 months ago

@cowgod Did you find a solution for this in the end? Im facing the same thing using uuids as my route model binding key.

Thanks