laravel路由无法访问,报404,No query results for model [App\Models\...]
生活随笔
收集整理的這篇文章主要介紹了
laravel路由无法访问,报404,No query results for model [App\Models\...]
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
今天遇到了一個(gè)問題,在routes/web.php中配置了路由,但始終無法訪問該路由,一直報(bào)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路由訪問檢測(cè)是從上到下的。
針對(duì)同路徑,gift_packs/{gift_pack} 和 gift_packs/test,當(dāng)我們?cè)L問 /gift_packs/test時(shí),路由 gift_packs/{gift_pack} 已經(jīng)解析了。
字符串 'test' 無法獲取GiftPack模型數(shù)據(jù),所以就報(bào)錯(cuò)了。
解決方法是修改路由配置的上下位置:
Route::get('gift_packs/test', 'GiftPacksController@test')->name('gift_packs.test'); Route::resource('gift_packs', 'GiftPacksController', ['only' => ['index', 'show', 'create', 'store', 'update', 'edit', 'destroy']]);這樣就可以了。
?
轉(zhuǎn)載于:https://www.cnblogs.com/jkko123/p/10773000.html
總結(jié)
以上是生活随笔為你收集整理的laravel路由无法访问,报404,No query results for model [App\Models\...]的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU 5306 Gorgeous Se
- 下一篇: C#通过Array.Clear部分清除数