【问题标题】:SQLSTATE[23000]: Integrity constraint violation: 1048 laravel 5.7SQLSTATE [23000]:完整性约束违规:1048 laravel 5.7
【发布时间】:2020-01-31 00:08:02
【问题描述】:

我在 Laravel 5.7 中遇到错误:

照亮\数据库\查询异常 SQLSTATE[23000]:完整性约束违规:1048 列“名称”不能为空(SQL:插入storesnamematricphoneemailpasswordupdated_atcreated_at) 值 (?, ?, ?, ?, ?, 2019-10-01 16:29:49, 2019-10-01 16:29:49))

这是我的表格:

<!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>

这是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('/');
    }
}

这是迁移:

<?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');
    }
}

【问题讨论】:

  • 您的输入没有name 属性,因此不会通过表单传递。

标签: javascript php laravel phpmyadmin laravel-5.7


【解决方案1】:

您可能正在以文本格式发送数据:

text pic
. json pic

【讨论】:

  • 请解释您的解决方案。这有助于 OP 和其他人准确了解问题所在。
  • 我在使用邮递员在数据库中发布数据时遇到了同样的问题,而在发布数据时,原始数据中的语言被选择为应该是 JSON 的 TEXT。那就是引发错误的错误。
【解决方案2】:

您还可以采取以下步骤来绕过您的 Laravel 网站中的此错误:-

  1. 在位于路径\config\database.php 文件中更新'strict' =&gt; false

  2. 在位于路径\app\Http\Kernel.php 文件中评论以下语句

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

希望将来能对某人有所帮助。

【讨论】:

    【解决方案3】:

    您应该使用name="namehere" 命名您的输入 例如

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

    【讨论】:

    • 这样,您可以通过 $request 在控制器中访问它。 $request->name 或者如果您将输入命名为“email”,那么 $request->email 也可以是 request('name') 等。
    【解决方案4】:

    您的 html 表单中缺少 name 属性。如果没有此属性,您的输入数据将不会传递给控制器​​,因此您将获得空值。所以将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>
    

    并且还要更改您的验证

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

    【讨论】:

      【解决方案5】:

      您正在尝试在 name 列中保存一个空值,即使它是必需的。

      【讨论】:

      • 那么我该如何解决我想将名称存储到数据库中
      • 我相信在从字段中检索值时发生错误,在 UserController 类中,尝试检查您是否可以检索输入值,或者它返回 null 或空。 --> $store-> name = $request->input('name');
      猜你喜欢
      • 2021-03-22
      • 2022-11-02
      • 1970-01-01
      • 2020-10-18
      • 2017-09-11
      • 1970-01-01
      • 2018-05-01
      • 2021-01-13
      • 2015-07-17
      相关资源
      最近更新 更多