不在laravel 5.6中将用户数据保存到用户表

时间:2018-08-02 04:54:19

标签: mysql laravel

我要在laravel 5.6中的用户表中插入新的数据记录。这是我的UserController,

public function store(Request $request)
    {
        $this->validate($request,[
         'name' => 'required|max:255',
         'userlevel' => 'required',
         'email' => 'required|email|unique:users',
]);
$user = new User();
        $user->name = $request->input('name');
        $user->uservalue = $request->input('uservalue');
        $user->email = $request->input('email');
        $user->password = $request->input(bcrypt($data['password']));
        $user->save();
        return redirect()->route('users.show', $user->id);
    }

刀片文件是

<form action="{{route('users.store')}}" method="POST">
            {{csrf_field()}}
            <div class="form-group">
                <label for="name">Name</label>
                <input type="text" class="form-control" id="name" name="name" placeholder="Enter Name">
            </div>

            <div class="form-group">
            <label for="exampleFormControlSelect1">User Level</label>
            <select class="form-control" id="uservalue" name="uservalue">
            <option>0</option>
            <option>2</option>
            <option>1</option>

            </select>
            </div>

            <div class="form-group">
                <label for="email">Email</label>
                <input type="text" class="form-control" id="email" name="email" placeholder="Enter Email">
            </div>

            <div class="form-group">
                <label for="password">Password</label>
                <input type="text" class="form-control" id="password" name="password" placeholder="Enter Password">

            </div>
            <button type="submit" class="btn btn-primary">Create User</button>
        </form>
        </div>

路线是

Route::resource('/users','UserController');

但是当我单击“提交”按钮数据时,未保存到用户表。没有发生任何错误消息。仅刷新页面。如何解决这个问题?

3 个答案:

答案 0 :(得分:4)

这可能是验证错误,不会显示在您的视图中。

将此代码添加到刀片文件中以显示验证错误。

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

答案 1 :(得分:3)

public function store(Request $request)
    {
        $this->validate($request,[
         'name' => 'required|max:255',
         'uservalue' => 'required',
         'email' => 'required|email|unique:users',
]);
$user = new User();
        $user->name = $request->input('name');
        $user->uservalue = $request->input('uservalue');
        $user->email = $request->input('email');
        $user->password = $request->input(bcrypt($data['password']));
        $user->save();
        return redirect()->route('users.show', $user->id);
    }

请将用户级别更改为uservalue

答案 2 :(得分:2)

我认为问题出在这里$request->input(bcrypt($data['password']))您正在传递$ data而不是对其进行定义。只需尝试:bcrypt($request->input('password'))

此外,您正在验证userlevel,但插入uservalue