SQLSTATE [23000]:违反完整性约束:1048列' EMAIL'不能为空

时间:2016-08-28 11:11:53

标签: php mysql laravel

我是laravel的新手,请给出解决方案 这是用户控制器代码,当我提交表单时,我收到的电子邮件是空的,但我正在插入电子邮件。

没有laravel问题我认为它的mysql问题

<?php
namespace App\http\Controllers;

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

     class UserController extends Controller
{
   public function postSignUp(Request $request)
   {
     $email = $request['email'];
     $first_name = $request['first_name'];
     $password = bcrypt($request['password']);

     $user = new User();
     $user->email = $email;
     $user->first_name = $first_name;
     $user->password = $password;

     $user-> save();

     return redirect()-> back();
   }

     public function postSignIn(Request $request)
   {
   }

   }

这是迁移表。

<?php

 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->timestamps();
        $table->string('email');
        $table->string('first_name');
        $table->string('password');
        $table->rememberToken();
    });
}

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

这是我的html文件

<div class="row">
  <div class="col-md-6">
  <h3>Sign Up</h3>
  <form action="{{ route('signup') }}" method="post">
  <div class="form-group">
  <lable for="email">Your Email</lable>
  <input class="form-control" type="email" name "email" id="email">
  </div>
  <div class="form-group">
  <lable for="first_name">Your first name</lable>
  <input class="form-control" type="text" name="first_name" id="first_name">
  </div><div class="form-group">
  <lable for="password">Your Password</lable>
  <input class="form-control" type="password" name "password" id="password">
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
  <input type="hidden" name="_token" value="{{ Session::token() }}">
  </form>
  </div>

这是我的路线

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

 Route::post('/signup', [
       'uses' => 'UserController@postSignUp',
       'as' => 'signup'
]);

3 个答案:

答案 0 :(得分:0)

  

你的HTML错了。   替换:

<input class="form-control" type="text" name "email" id="email">
<input class="form-control" type="password" name "email" id="password">

<input class="form-control" type="email" name="email" id="email">
<input class="form-control" type="password" name="password" id="password">

之后更换你的控制器:

$email= $request->input( 'email' );

答案 1 :(得分:0)

要获取电子邮件值,您应先添加= btw名称和“电子邮件”,然后在控制器内部将$email = $request['email']替换为$email = $request->input('email');

答案 2 :(得分:0)

您好,问题是您正在使用

$email = $request['email'];

如果像下面这样为空,则必须传递一个默认值,或者使mysql字段接受空值

$name = Input::get('email', 'default@gmail.com');