404 not found 错误仅在一个路由中 Laravel 7

404 not found error only in one route Laravel 7

我正在重构我的代码并在一条路径中收到 404 未找到页面 错误。我尝试了所有可能的解决方案,但没有运气。我的路线如下:

Route::prefix('admin')->group(function () {
    .... other routes
    
    Route::prefix('product')->group(function () {
        .... other routes

        Route::prefix('category')->group(function () {
            Route::get('/', function () {
                dd('check');
            });

            <!-- Route::get('/', 'ProductCategoryController@index')->name('product_category_index'); -->
           
            .... other routes

        });
    });
});

在调试栏中出现异常:

模型 [App\Product] 类别无查询结果 F:\bvend\bvend.web\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php#389 Illuminate\Database\Eloquent\ModelNotFoundException

我的代码中不再有 App\Category 模型。相反,我有 App\Product类别

我不知道错误是什么。请帮忙

问题是两条路由相互冲突。

假设您有以下两条路线,顺序如下:

admin/product/{product}

admin/product/category

当您尝试访问 admin/product/category 时,您实际上是在使用 category 作为路由参数 {product}.

的值访问 admin/product/{product}

这就是您收到错误 No query results for model [App\Product] category 的原因,它正在尝试搜索 ID 为 category 的产品。

现在,如果您更改顺序:

admin/product/category

admin/product/{product}

现在路由 admin/product/category 的优先级高于 admin/product/{product},因此您实际上可以访问所需的路由,而不是匹配到 admin/product/{product} 路由。