未知 Laravel 异常

Unknown Laravel Exception

我正在我的 Vue.js 应用程序和 Laravel 之间设置路由。我已经 setup CSRF with Vue.js,但是 Laravel 现在在我尝试使用我的 PageController@store 方法时抛出以下错误。它似乎与我的 PageController 中的类型提示有关:我删除了所有类型提示,此错误消失了。但是,我没有收到任何返回的数据。

"message": "No query results for model [App\Page]." "exception": Symfony\Component\HttpKernel\Exception\NotFoundHttpException",

这是出乎意料的,因为我的存储方法仅 returns 请求为 JSON(我也尝试过使用空存储方法)。

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    return response()->json($request);
}

这是我的路线:

Route::resource('/admin/resource/pages', 'PageController');

最后,这是我的 Vue.js 方法,它将数据发送到 Laravel。

post (model){
    this.$http.put("/admin/resource/pages/store", {
        _method: "put",
        model: model
    }, {}).then(response => {
        console.log(response);
    });
}

感谢任何帮助,谢谢。

错误堆栈已满:

{
    "message": "No query results for model [App\Page].",
    "exception": "Symfony\Component\HttpKernel\Exception\NotFoundHttpException",
    "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php",
    "line": 200,
    "trace": [
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php",
            "line": 176,
            "function": "prepareException",
            "class": "Illuminate\Foundation\Exceptions\Handler",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/app/Exceptions/Handler.php",
            "line": 49,
            "function": "render",
            "class": "Illuminate\Foundation\Exceptions\Handler",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 83,
            "function": "render",
            "class": "App\Exceptions\Handler",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 55,
            "function": "handleException",
            "class": "Illuminate\Routing\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php",
            "line": 75,
            "function": "Illuminate\Routing\{closure}",
            "class": "Illuminate\Routing\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 151,
            "function": "handle",
            "class": "Illuminate\Foundation\Http\Middleware\VerifyCsrfToken",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\Pipeline\{closure}",
            "class": "Illuminate\Pipeline\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php",
            "line": 49,
            "function": "Illuminate\Routing\{closure}",
            "class": "Illuminate\Routing\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 151,
            "function": "handle",
            "class": "Illuminate\View\Middleware\ShareErrorsFromSession",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\Pipeline\{closure}",
            "class": "Illuminate\Pipeline\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php",
            "line": 63,
            "function": "Illuminate\Routing\{closure}",
            "class": "Illuminate\Routing\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 151,
            "function": "handle",
            "class": "Illuminate\Session\Middleware\StartSession",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\Pipeline\{closure}",
            "class": "Illuminate\Pipeline\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php",
            "line": 37,
            "function": "Illuminate\Routing\{closure}",
            "class": "Illuminate\Routing\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 151,
            "function": "handle",
            "class": "Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\Pipeline\{closure}",
            "class": "Illuminate\Pipeline\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php",
            "line": 66,
            "function": "Illuminate\Routing\{closure}",
            "class": "Illuminate\Routing\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 151,
            "function": "handle",
            "class": "Illuminate\Cookie\Middleware\EncryptCookies",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\Pipeline\{closure}",
            "class": "Illuminate\Pipeline\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 104,
            "function": "Illuminate\Routing\{closure}",
            "class": "Illuminate\Routing\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 681,
            "function": "then",
            "class": "Illuminate\Pipeline\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 656,
            "function": "runRouteWithinStack",
            "class": "Illuminate\Routing\Router",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 622,
            "function": "runRoute",
            "class": "Illuminate\Routing\Router",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
            "line": 611,
            "function": "dispatchToRoute",
            "class": "Illuminate\Routing\Router",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 176,
            "function": "dispatch",
            "class": "Illuminate\Routing\Router",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 30,
            "function": "Illuminate\Foundation\Http\{closure}",
            "class": "Illuminate\Foundation\Http\Kernel",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/fideloper/proxy/src/TrustProxies.php",
            "line": 57,
            "function": "Illuminate\Routing\{closure}",
            "class": "Illuminate\Routing\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 151,
            "function": "handle",
            "class": "Fideloper\Proxy\TrustProxies",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\Pipeline\{closure}",
            "class": "Illuminate\Pipeline\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
            "line": 31,
            "function": "Illuminate\Routing\{closure}",
            "class": "Illuminate\Routing\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 151,
            "function": "handle",
            "class": "Illuminate\Foundation\Http\Middleware\TransformsRequest",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\Pipeline\{closure}",
            "class": "Illuminate\Pipeline\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
            "line": 31,
            "function": "Illuminate\Routing\{closure}",
            "class": "Illuminate\Routing\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 151,
            "function": "handle",
            "class": "Illuminate\Foundation\Http\Middleware\TransformsRequest",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\Pipeline\{closure}",
            "class": "Illuminate\Pipeline\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php",
            "line": 27,
            "function": "Illuminate\Routing\{closure}",
            "class": "Illuminate\Routing\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 151,
            "function": "handle",
            "class": "Illuminate\Foundation\Http\Middleware\ValidatePostSize",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\Pipeline\{closure}",
            "class": "Illuminate\Pipeline\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php",
            "line": 62,
            "function": "Illuminate\Routing\{closure}",
            "class": "Illuminate\Routing\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 151,
            "function": "handle",
            "class": "Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php",
            "line": 53,
            "function": "Illuminate\Pipeline\{closure}",
            "class": "Illuminate\Pipeline\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
            "line": 104,
            "function": "Illuminate\Routing\{closure}",
            "class": "Illuminate\Routing\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 151,
            "function": "then",
            "class": "Illuminate\Pipeline\Pipeline",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
            "line": 116,
            "function": "sendRequestThroughRouter",
            "class": "Illuminate\Foundation\Http\Kernel",
            "type": "->"
        },
        {
            "file": "/tempfiles/75/blog/public/index.php",
            "line": 55,
            "function": "handle",
            "class": "Illuminate\Foundation\Http\Kernel",
            "type": "->"
        }
    ]
}

页面控制器:

<?php

namespace App\Http\Controllers;

use App\Page;
use Illuminate\Http\Request;

class PageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // return response()->json(Page::all());
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        return response()->json($request);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Page  $page
     * @return \Illuminate\Http\Response
     */
    public function show(Page $page)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Page  $page
     * @return \Illuminate\Http\Response
     */
    public function edit(Page $page)
    {
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Page  $page
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Page $page)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Page  $page
     * @return \Illuminate\Http\Response
     */
    public function destroy(Page $page)
    {
        //
    }
}

当您使用 put 方法时,实际上您正在点击:

public function update(Request $request, Page $page)
{
    //
}

控制器方法。因为这里有 Page $page 作为参数,所以 Laravel 自动使用路由模型绑定 运行:

Page::findOrFail($page);

因为你在 Vue 中设置 url 为 /admin/resource/pages/store 事实上它会 运行:

Page::findOrFail('store');

当然,我假设您没有任何 id = 'store' 的页面记录,所以这就是您收到此错误的原因。

所以您可能应该在 Vue 中更正您的路线,以传递有效 ID 而不是 store 或使用其他方法,例如 POST 和有效路线。

在控制台中你可以运行:

php artisan route:list

查看所有可用路线。