简体   繁体   English

SQLSTATE [23000]:违反完整性约束:1048 laravel 5.7

[英]SQLSTATE[23000]: Integrity constraint violation: 1048 laravel 5.7

I am getting error in Laravel 5.7:我在 Laravel 5.7 中遇到错误:

Illuminate\Database\QueryException SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null (SQL: insert into stores ( name , matric , phone , email , password , updated_at , created_at ) values (?, ?, ?, ?, ?, 2019-10-01 16:29:49, 2019-10-01 16:29:49)) Illuminate\Database\QueryException SQLSTATE[23000]:完整性约束违规:1048 列“名称”不能为created_at (SQL:插入storesnameemailphonematricpassword ?,更新时间?创建updated_at ), , ?, ?, 2019-10-01 16:29:49, 2019-10-01 16:29:49))

This is my form:这是我的表格:

<!DOCTYPE html>
<html>
<head>
    @section('title', 'Sign up for customer page')
</head>
<body class="text-center">
@extends('layout.app')

@section('content')

@endsection
@include('include.navbar')
<form class="form-signup" action="{{URL:: to('/store')}}" method="post">
  @csrf

  <h1 class="h3 mb-3 font-weight-normal">Please sign up as customer</h1>
  <label for="inputname" class="sr-only">Name</label>
  <input type="text" id="inputname" class="form-control" placeholder="Name" required>
  <label for="inputmatric" class="sr-only">ID matric</label>
  <input type="text" id="inputmatric" class="form-control" placeholder="ID matric" required>
  <label for="inputphon" class="sr-only">Phone No</label>
  <input type="text" id="inputphon" class="form-control" placeholder="Phone No" required>
  <label for="inputemail" class="sr-only">E-mail</label>
  <input type="text" id="inputemail" class="form-control" placeholder="E-mail" required>
  <label for="inputpassword" class="sr-only">Password</label>
  <input type="text" id="inputpassword" class="form-control" placeholder="Password" required>


  <button class="btn btn-lg btn-primary btn-block"  type="submit">Sign up</button>
  <p class="mt-5 mb-3 text-muted">&copy; 2017-2019</p>
</form>

</html>

This is UserController :这是UserController

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\DB;
use App\store;//model name

class UserController extends Controller
{
    public function store(Request $request)
    {

        $this->validate($request,[
            'name'=> 'request',
            'matric'=> 'request',
            'phone'=> 'request',
            'email'=>'request',
            'password'=> 'request'

        ]);

        //store new customer
        $store = new store;   // valible and model name
        $store-> name = $request->input('name');
        $store-> matric = $request->input('matric');
        $store-> phone = $request->input('phone');
        $store-> email = $request->input('email');
        $store-> password = $request->input('password');

        //save new customer
        $store-> save();

        //redirect
        return redirect('/');
    }
}

This is the Migration:这是迁移:

<?php
//create the customer table
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateStoresTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('stores', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string ('name');
            $table->integer ('matric');
            $table->string ('phone');
            $table->string ('email');
            $table->string ('password');
            $table->timestamps();
        });
    }

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

You are trying to save a null value in the name column even though it is required.您正在尝试在名称列中保存 null 值,即使它是必需的。

You are missing name attribute in your html form.您的 html 表单中缺少name属性。 Without this attribute your input data won't be passed to the controller and thus you are getting empty values.如果没有此属性,您的输入数据将不会传递给 controller,因此您将获得空值。 So add the name attribute to the input fields.因此,将name属性添加到输入字段。

<label for="inputname" class="sr-only">Name</label>
<input type="text" id="inputname" name="name" class="form-control" placeholder="Name" required>
<label for="inputmatric" class="sr-only">ID matric</label>
<input type="text" id="inputmatric" name="matric" class="form-control" placeholder="ID matric" required>
<label for="inputphon" class="sr-only">Phone No</label>
<input type="text" id="inputphon" name="phone" class="form-control" placeholder="Phone No" required>
<label for="inputemail" class="sr-only">E-mail</label>
<input type="text" id="inputemail" name="email" class="form-control" placeholder="E-mail" required>
<label for="inputpassword" class="sr-only">Password</label>
<input type="text" id="inputpassword" name="password" class="form-control" placeholder="Password" required>

And also change your validation并更改您的验证

$this->validate($request,[
    'name'=> 'required',
    'matric'=> 'required',
    'phone'=> 'required',
    'email'=>'required',
    'password'=> 'required'
]);

You can also take below steps to bypass this error in your Laravel website:-您还可以采取以下步骤绕过 Laravel 网站中的此错误:-

  1. Update 'strict' => false in database.php file located at path \config\在位于路径\config\database.php文件中更新'strict' => false

  2. Comment below statement in Kernel.php file located at path \app\Http\在位于路径\app\Http\Kernel.php文件中的语句下方评论

// \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, // \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,

Hope it will help someone in future.希望它将来对某人有所帮助。

You should name your input with name="namehere" eg您应该使用name="namehere"命名您的输入,例如

<input type="text" id="inputname" name="name" class="form-control" placeholder="Name" required>

You might be sending data in text format:您可能以文本格式发送数据:

. . text pic文字图片
. . json pic json图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM