如何在laravel中存储表单数据?

路线/web.php

Route::get('/','UserController@index');
Route::post('/index','UserController@store');

控制器:UserController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Users;

class UserController extends Controller
{
    public function index()
    {
        return view('index');
    }

    public function store(Request $request)
    {
        $user = new Users();
        $user->fname = $request->input('fname');
        $user->lname = $request->input('lname');
        $user->email = $request->input('email');
        $user->password = $request->input('password');
        $user->save();
        return redirect()->route('index')->with('info','User Added Successfully');
    }
}

查看:index.blade.php

@extends('layout.main')
@section('title','Index Page')
@section('content')
    <div class="container">
        <div class="row">
            <form id="myform" action="{{route('store')}}">
                @csrf
                <div class="form-group">
                    <label for="fname">{{ trans('core.fname') }}</label>
                    <input type="text" class="form-control" id="fname" name="fname"/>
                </div>
                <div class="form-group">
                    <label for="fname">{{ trans('core.lname') }}</label>
                    <input type="text" class="form-control" id="lname" name="lname"/>
                </div>
                <div class="form-group">
                    <label for="fname">{{ trans('core.email') }}</label>
                    <input type="text" class="form-control" id="email" name="email"/>
                </div>
                <div class="form-group">
                    <label for="fname">{{ trans('core.password') }}</label>
                    <input type="password" class="form-control" id="password" name="password"/>
                </div>
                <div class="form-group">
                    <input type="submit" class="btn btn-primary" id="submit" name="submit" value="{{trans('core.submit')}}"/>
                </div>
            </form>
        </div>
    </div>
@endsection

我是Laravel新手。在此代码中,我只是想执行CRUD操作。在这里,只有我正在执行插入操作,但它向我显示错误,即

路线[商店]未定义。 (视图:   C:\ xampp \ htdocs \ myproject \ resources \ views \ index.blade.php)。

我没有错在哪里?那么,我该怎么做?请帮我。

谢谢

评论
  • 自惭形秽、
    自惭形秽、 回复

    Change your web.php like enter code here

    Route::resource('index','UserController');
    

    更改

    use App\User;
    
    $user = new User();