简体   繁体   中英

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. I get MethodNotAllowedHttpException on line 251 when I submit my form. This is normally related to not matching a post in web.php with a post form, but adding this hasn't resolved anything.

I believe everything is declared correctly but would appreciate any pointers if you see anything amiss.

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

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

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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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