'Laravel 9 edit route with {id} parameter returns Not Found (404) despite being defined

Below is an example of the route definition in my routes/web.php file.

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\WidgetController;

Route::controller(WidgetController::class)->name('widgets.')->prefix('widgets')->group(function () {
  Route::get('{id}/{slug}', 'show')->name('show');
  Route::get('create', 'create')->name('create')->middleware('auth');
  Route::post('store', 'store')->name('store')->middleware('auth');
  Route::get('{id}/edit', 'edit')->name('edit')->middleware('auth');
  Route::post('update', 'update')->name('update')->middleware('auth');
});

All the routes listed work fine except for one.

Route::get('{id}/edit', 'edit')->name('edit')->middleware('auth');

For whatever reason, it works completely fine when it's written like this:

Route::get('{id}', 'edit')->name('edit')->middleware('auth');

or this:

Route::get('{id}//edit', 'edit')->name('edit')->middleware('auth');

With "Route::resource", the default would be the first "edit" route. However, I get a 404 when defining it like that. My associated controller method is written like this:

public function edit($id)
{
   // Rest of code here
}

Could it be that Laravel is mistaking "edit" as a second parameter? But that doesn't really make much sense considering it's the default for the resource method.



Solution 1:[1]

Try putting

Route::get('{id}/edit', 'edit')->nam

Above

Route::get('{id}/{slug}'

Since {slug} could be anything it's more then likely getting executed first. I would suggest putting that last.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 josezenem