插入数据时为什么会收到Route Collection错误?

时间:2016-12-13 17:31:26

标签: laravel laravel-5.3

当我尝试在Laravel 5.3中插入数据时显示错误

哎呀,好像出了什么问题。 RouteCollection.php第218行中的1/1 MethodNotAllowedHttpException:

in RouteCollection.php line 218
at RouteCollection->methodNotAllowed(array('GET', 'HEAD')) in RouteCollection.php line 205
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 755
at Router->findRoute(object(Request)) in Router.php line 610
at Router->dispatchToRoute(object(Request)) in Router.php line 596
at Router->dispatch(object(Request)) in Kernel.php line 268
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Kernel.php line 150
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 117
at Kernel->handle(object(Request)) in index.php line 53

我的控制器代码

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\userCreate;

public function store(Request $request)
    {

        $newUser = new userCreate;

        $newUser ->fname = $request->fname;
        $newUser ->lname = $request->lname;
        $newUser ->email = $request->email;
        $newUser ->password = $request->password;
        $newUser ->utype = $request->utype;

        $newUser->save();

       //return redirect('/');
    }

我希望在这里发布我的路线:

4 个答案:

答案 0 :(得分:1)

在访问创建新用户的路线时,请确保您的方法是POST。

答案 1 :(得分:1)

尝试一下......我认为您正在使用资源控制器,因此路由可能没问题。

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\userCreate;

public function store(Request $request)
    {

        $newUser = new userCreate;

        $newUser ->fname = $request->input('fname');
        $newUser ->lname = $request->input('lname');
        $newUser ->email = $request->input('email');
        $newUser ->password = $request->input('password');
        $newUser ->utype = $request->input('utype');

        $newUser->save();

       //return redirect('/');
    }

答案 2 :(得分:1)

访问创建新用户的路线时使用POST方法。

Route::post('/store',    'NameController@store');

答案 3 :(得分:1)

您的路线应如下所示:

Route::resource('store','ControllerName');

如果仍无效,请将routes.php添加到问题。