簡體   English   中英

SQLSTATE [23000]:違反完整性約束:1048 laravel 5.7

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

我在 Laravel 5.7 中遇到錯誤:

Illuminate\Database\QueryException SQLSTATE[23000]:完整性約束違規:1048 列“名稱”不能為created_at (SQL:插入storesnameemailphonematricpassword ?,更新時間?創建updated_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');
    }
}

您正在嘗試在名稱列中保存 null 值,即使它是必需的。

您的 html 表單中缺少name屬性。 如果沒有此屬性,您的輸入數據將不會傳遞給 controller,因此您將獲得空值。 因此,將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'
]);

您還可以采取以下步驟繞過 Laravel 網站中的此錯誤:-

  1. 在位於路徑\config\database.php文件中更新'strict' => false

  2. 在位於路徑\app\Http\Kernel.php文件中的語句下方評論

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

希望它將來對某人有所幫助。

您應該使用name="namehere"命名您的輸入,例如

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

您可能以文本格式發送數據:

. 文字圖片
. json圖片

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM