Laravel:Field' remember_me'没有默认值

时间:2018-05-13 09:57:20

标签: javascript laravel

我正在创建一个CMS应用程序,当然用户需要首先注册或登录。

但是在将数据发送到数据库时遇到了问题。它说'Field 'remember_me' doesn't have a default value'

我无法找到为什么它需要默认值,因为我发送了remember_me数据,即checked => 1和unchecked => 0!我正在使用Javascript!

RegisterController.php

<?php

namespace App\Http\Controllers\Authentication;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;

class RegisterController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index() {
        return view('auth.register');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create() {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request) {
        $user = User::create($request->all());

        return redirect('/');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id) {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id) {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id) {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id) {
        //
    }
}

register.blade.php

@extends('master')

@section('title')
  Register - CMS APP
@endsection

@section('styles')
  <link rel="stylesheet" href="{{ asset('css/auth.css') }}">
@endsection

@section('register')
  <form class="form-signin" method="post" accept-charset="utf8" action="/register">
    @csrf
    <img class="mb-4" src="http://logo.kenh.net/logo/bootstrap-4.svg.png" alt="" width="72" height="72">
    <h1 class="h3 mb-3 font-weight-normal">Please Register</h1>
    <label for="inputFirstName" class="sr-only">First Name</label>
    <input name="fname" type="string" id="inputFirstName" class="form-control" placeholder="First Name" required autofocus>
    <label for="inputLastName" class="sr-only">Last Name</label>
    <input name="lname" type="string" id="inputLastName" class="form-control" placeholder="Last Name" required autofocus>
    <label for="inputEmail" class="sr-only">Email address</label>
    <input name="email" type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
    <label for="inputPassword" class="sr-only">Password</label>
    <input name="password" type="password" id="inputPassword" class="form-control" placeholder="Password" required>
    <div class="checkbox mb-3">
      <label>
        <input name="remember_me" id="rememMe" type="checkbox" value="0"> Remember me
      </label>
    </div>
    <button class="btn btn-lg btn-success btn-block" type="submit">Register</button>
    <br>
    <a href="/login" class="btn btn-primary">Login</a>
  </form>
@endsection

<script>
  window.onload = () => {
    let rememberMe = document.getElementById('rememMe');

    rememberMe.onclick = () => {
      if(rememberMe.checked) {
        rememberMe.value = 1;
      }else {
        rememberMe.value = 0;
      }
    }
  };
</script>

路线

Route::resource('register', 'Authentication\RegisterController')->only([
    'index', 'store'
]);

迁移:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('role_id')->default(2);
            $table->integer('is_active')->default(1);
            $table->integer('remember_me');
            $table->string('fname');
            $table->string('lname');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

请帮我解决一下!

2 个答案:

答案 0 :(得分:0)

我认为复选框的值应为1,因为当您提交时,即使已经选中它也会为0

<input name="remember_me" id="rememMe" type="checkbox" value="1">

并确保错误再次发生,您可以在控制器中执行此操作

public function store(Request $request) {
    $request['remember_me'] = $request['remember_me'] ? 1 : 0;
    $user = User::create($request->all());

    return redirect('/');
}

希望这会有所帮助

答案 1 :(得分:0)

如果在表单中选中了复选框,则会提供on的值,而不是1数据库中的字段存储integer值,你应该真的使用boolean。这样,Eloquent将接受并存储来自onoff的值,并将其输入为10

以后要防止此错误,您应该在字段中添加默认值,并将其存储为布尔值。

创建新的迁移。

php artisan make:migration add_default_remember_me_to_users

然后在迁移中:

public function up()
{
    Schema::table('users', function($table) {
        // Change field to boolean and define a default value
        $table->boolean('remember_me')->default(0)->change();
    });
}

public function down()
{
    Schema::table('users', function($table) {
        $table->integer('remember_me')->change();
    });

}

然后运行迁移:

php artisan migrate

你应该好好去!