【问题标题】:404 not found error only in one route Laravel 7仅在一条路线 Laravel 7 中未找到 404 错误
【发布时间】:2020-10-27 21:07:10
【问题描述】:

我正在重构我的代码并在一个路由中遇到 404 Page not found 错误。我尝试了所有可能的解决方案,但没有运气。我的路线如下:

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\ProductCategory

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

【问题讨论】:

  • 你是如何尝试访问这条路线的?
  • 您是否有类似这样的路线:admin/product/{product}?对我来说似乎是路线冲突,也许可以尝试将Route::prefix('category') 直接放在Route::prefix('product') 之后
  • 这是类不匹配,运行composer dump-autoload希望问题消失
  • 嘿!它就像魅力一样……你能告诉我冲突在哪里吗?似乎只是更改了代码顺序。非常感谢@Remul

标签: laravel laravel-5 laravel-7


【解决方案1】:

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

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

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} 路由。

【讨论】:

  • 很好的解释。 !!
  • @Remul,解决办法是什么?
  • @abu-abu 更改路线的顺序可以解决问题
  • @Remul 我是 laravel 的新手,我到处寻找解决方案。谢谢你
猜你喜欢
  • 2015-02-04
  • 2020-04-05
  • 1970-01-01
  • 2018-10-23
  • 2016-05-16
  • 2019-05-20
  • 2016-09-11
  • 2021-01-06
  • 2017-06-17
相关资源
最近更新 更多