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

时间:2019-10-01 16:48:48

标签: javascript php laravel phpmyadmin laravel-5.7

我在Laravel 5.7中遇到错误:

  

Illuminate \ Database \ QueryException   SQLSTATE [23000]:违反完整性约束:1048列“名称”不能为空(SQL:插入storesnamematricphone,{{1} },emailpasswordupdated_at)值(?,?,?,?,?,2019-10-01 16:29:49、2019-10-01 16: 29:49))

这是我的表格:

created_at

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

5 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您缺少html表单中的df1 <- structure(list(A1 = c("So,Smith", "Jo, Man", "So,Smith", "Jo, Man", "Rose,Moli", "Goli,Gouzo", "Jo, Man", "Rose,Moli", "Goli,Gouzo" ), A2 = c("Amo, Bazse", "Amo, Bazse", "Hosh,Golab", "Jo, Man", "Niss, Peak", "Ebi,Sat", "Ebi,Sat", "Niss, Peak", "Jo, Man"), A3 = c("Arzo, Ghas", "Imran, Maz", "Imran, Maz", "Imran, Maz", "Arzo, Ghas", "Imran, Maz", "Imran, Maz", "Arzo, Ghas", "Arzo, Ghas" )), class = "data.frame", row.names = c(NA, -9L)) 属性。没有此属性,您的输入数据将不会传递到控制器,因此您将获得空值。因此,将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>

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

您还可以按照以下步骤绕过Laravel网站中的此错误:-

  1. 更新位于路径'strict' => false的{​​{1}}文件中的database.php

  2. 在位于路径\config\的{​​{1}}文件中的以下语句中进行注释:

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

希望它将对以后的人有所帮助。

答案 4 :(得分:0)

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

text pic
. json pic