日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

laravel路由无法访问,报404,No query results for model [App\Models\...]

發布時間:2025/7/14 72 豆豆
生活随笔 收集整理的這篇文章主要介紹了 laravel路由无法访问,报404,No query results for model [App\Models\...] 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今天遇到了一個問題,在routes/web.php中配置了路由,但始終無法訪問該路由,一直報404。

Route::resource('gift_packs', 'GiftPacksController', ['only' => ['index', 'show', 'create', 'store', 'update', 'edit', 'destroy']]); Route::get('gift_packs/test', 'GiftPacksController@test')->name('gift_packs.test');

然后我在app/Exceptions/Handler.php文件中,修改render()方法:

public function render($request, Exception $exception) {dd($exception);return parent::render($request, $exception); }

把異常打印出來:

No query results for model [App\Models\GiftPack].

先通過?php artisan route:list 查看路由列表

| Domain | Method | URI | Name | | | GET|HEAD | gift_packs/{gift_pack} | gift_packs.show | | | DELETE | gift_packs/{gift_pack} | gift_packs.destroy | | | PUT|PATCH | gift_packs/{gift_pack} | gift_packs.update | | | GET|HEAD | gift_packs/{gift_pack}/edit | gift_packs.edit | | | GET|HEAD | gift_packs/test | gift_packs.test |

原因是laravel路由訪問檢測是從上到下的。

針對同路徑,gift_packs/{gift_pack} 和 gift_packs/test,當我們訪問 /gift_packs/test時,路由 gift_packs/{gift_pack} 已經解析了。

字符串 'test' 無法獲取GiftPack模型數據,所以就報錯了。

解決方法是修改路由配置的上下位置:

Route::get('gift_packs/test', 'GiftPacksController@test')->name('gift_packs.test'); Route::resource('gift_packs', 'GiftPacksController', ['only' => ['index', 'show', 'create', 'store', 'update', 'edit', 'destroy']]);

這樣就可以了。

?

  

轉載于:https://www.cnblogs.com/jkko123/p/10773000.html

總結

以上是生活随笔為你收集整理的laravel路由无法访问,报404,No query results for model [App\Models\...]的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。