Laravel:如何创建一个POST请求控制器并将用户保存在数据库中?

I have a problem with inserting Data into Database.

All i have done till now is :

Create a Model with Controller and Migration.

So, now my files looks this way :

UserController - Controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return response()->json([
            'name' => 'Abigail',
            'state' => 'CA'
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $user = new User;
        $user->first_name = $request->first_name;
        $user->last_name = $request->last_name;
        $user->role = 'user';
        $user->slug = $userSlug;
        $user->save();
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */

web.php - Routing:

<?php

Route::get('/', function () {
    return view('welcome');
});

Route::resource('users', 'UserController');
Route::post('users', 'UserController@create');

Couple of things that I've noticed are:

You use $request but it is never initialized not even passed in the method as a parameter, so you should change your method signature to this:

public function create(Request $request)

Next thing is you use $userSlug which again is not initialized.

And in your routes you use a resource and then you redeclare the create as separate. So in REST, create is used to represent the creation form, so this is a get method type, and then the store action method is the one to which you pass the form to be saved in the Database.

Please share your view and the error that you get so we have better picture on what is going on.

The error returned is that it does not find a controller create. This is the controller I found on the internet. So how should it be written?