SQLSTATE [23000]:违反完整性约束:19 NOT NULL 约束失败:

Laravel 的新手,试图让用户能够创建帖子。当我单击提交时,我收到此错误:


SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: posts.title (SQL: insert into "posts" ("user_id", "updated_at", "created_at") values (1, 2020-01-23 04:19:50, 2020-01-23 04:19:50))

我见过与此类似的问题,但没有一个得到很好的答案。这是我的模型:


<?php


namespace App;


use Illuminate\Database\Eloquent\Model;


class Post extends Model

{

    protected $gaurded = [];


    protected $fillable = ['title', 'thought', 'image', 'url'];


    public function user(){ 


        return $this->belongsTo(User::class);

    }

}


创建.blade.php:


@extends('layouts.app')


@section('content')


<body>



    <div class="container">


        <form action="/p" enctype="multipart/form-data" method="post">


            @csrf


        <div class="row">

            <div class="col-8 offset-2">

                <h1>New Post:</h1>

                <div class="form-group row">

                            <label for="title" class="col-md-4 col-form-label">Title</label>



                                <input id="title" name="title" type="text" class="form-control @error('title') is-invalid @enderror" title="title" value="{{ old('title') }}" required autocomplete="title" autofocus>



                            </div>


            </div>

        </div>


              <div class="row">

             <div class="col-8 offset-2">


                <div class="form-group row">

            <label for="thoughts" class="col-md-4 col-form-label">Thoughts</label>

                    <textarea type="text" class="form-control @error('thoughts') is-invalid @enderror" id="thoughts" name="thoughts"></textarea>  

               @error('file')

                                    <span class="invalid-feedback" role="alert">

                                        <strong>{{ $message }}</strong>

                                    </span>

                                @enderror

                 </div>

            </div>

        </div>


千万里不及你
浏览 263回答 2
2回答

拉风的咖菲猫

您缺少用于标题输入的 name="title"//... <input&nbsp;id="title"&nbsp;name="title"&nbsp;type="text"&nbsp;class="form-control&nbsp;@error('title')&nbsp;is-invalid&nbsp;@enderror"&nbsp;title="title"&nbsp;value="{{&nbsp;old('title')&nbsp;}}"&nbsp;required&nbsp;autocomplete="title"&nbsp;autofocus>更新您还缺少验证规则检查文档//...$validatedData = $request->validate([&nbsp; &nbsp; &nbsp; &nbsp; 'title' => 'required|unique:posts|max:255',&nbsp; &nbsp; &nbsp; &nbsp; 'thought' => 'required',&nbsp; &nbsp; &nbsp; &nbsp; 'image' => 'required|image',&nbsp; &nbsp; &nbsp; &nbsp; 'url' => 'required|url']);auth()->user()->posts()->create($validatedData);

幕布斯6054654

您可以通过两种类型来解决。您可以在数据库中设置空值 NULL(是)&nbsp;使用名称=“标题”
打开App,查看更多内容
随时随地看视频慕课网APP