简体   繁体   English

Laravel 5.4-已经登录时创建另一个用户帐户

[英]Laravel 5.4 - Create Another User Account while already logged in

I'm learning Laravel at the moment and am stuck adding user accounts from an existing account. 我目前正在学习Laravel,并且一直无法从现有帐户添加用户帐户。 I get MethodNotAllowedHttpException on line 251 when I submit my form. 提交表单时,在第251行出现MethodNotAllowedHttpException This is normally related to not matching a post in web.php with a post form, but adding this hasn't resolved anything. 通常,这与不将web.php中的帖子与帖子形式相匹配有关,但是添加此帖子并不能解决任何问题。

I believe everything is declared correctly but would appreciate any pointers if you see anything amiss. 我相信一切都已正确声明,但是如果您发现任何错误,将不胜感激。

adduser.blade adduser.blade

<div id="form-header">
    Create another user account
</div>
<div id="form-container">
    <form class="form-horizontal" method="POST">
    {{ csrf_field() }}
    <!--form variable-->
        <div class="form-group">
            <!-- form inputs -->
        </div>

        <div class=" form-group">
            <button type="submit">
                Create
            </button>
        </div>
    </form>
</div>

web.php web.php

Route::get('/adduser', 'AddUserController@index');
Route::post('/adduser', 'AddUserController@create_user');

addusercontroller addusercontroller

public function index()
{
    return view('adduser');
}

public function create_user(Request $request)
{
    return User::create(['input' => '$request->input']);
}

You've specified that the form should be a POST, but you haven't specified the route to post to. 您已指定该表单应为POST,但尚未指定要发布到的路由。

Change 更改

<form class="form-horizontal" method="POST">

to

<form class="form-horizontal" action="{{url('adduser')}}" method="POST">

I had two errors here, although the second wasn't included in my question (fatal throwable error I think). 我这里有两个错误,尽管我的问题中没有包含第二个错误(我认为是致命的可抛出错误)。 I had to add use app/user in the adduser controller, and for the initial routing error this was human error, I had two blades with the same name but one had a capital, and I was editing the wrong file. 我必须在adduser控制器中添加use app / user,对于最初的路由错误,这是人为错误,我有两个名称相同的刀片,但其中一个带有大写字母,并且编辑了错误的文件。 I've deleted this and put in the code from above in the correct blade which worked. 我已经删除了它,并将代码从上面放进了起作用的正确刀片中。

Just a personal note: You don't need to declare the method action if you're working from the current url as was mentioned in the comments. 只是个人注意:如果您使用注释中提到的当前URL,则无需声明方法操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM