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

时间:2020-01-23 05:10:06

标签: php sql laravel

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

create.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>

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

                <div class="form-group row">
            <label for="image" class="col-md-4 col-form-label">Image</label>
            <input type="file" class="form-control-file" id="image" name="image">  
               @error('file')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                 </div>
            </div>
        </div> 



           <div class="row pt-4">
             <div class="col-8 offset-2">

                <div class="form-group row">
                    <button class="btn btn-primary">Add post</button>
                 </div>
               </div>
            </div>

        </form>

    </div>

</body>


@endsection

PostsController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostsController extends Controller
{
    public function create(){

        return view('posts.create');
    }

    public function store(){

        $data = request()->validate([
            'title',
            'thought',
            'image',
            'url'
        ]);

        auth()->user()->posts()->create($data);

        dd(request()->all());
    }
}

最后是表格:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->text('title');
            $table->text('thought');
            $table->string('url')->nullable();
            $table->string('image')->nullable();
            $table->timestamps();

            $table->index('user_id');
        });
    }

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

任何建议都会很棒。如果您需要其他任何信息,请告诉我。

2 个答案:

答案 0 :(得分:1)

您缺少用于标题输入的name =“ title”

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

更新

您也缺少验证规则

选中docs

//...
$validatedData = $request->validate([
        'title' => 'required|unique:posts|max:255',
        'thought' => 'required',
        'image' => 'required|image',
        'url' => 'required|url'
]);

auth()->user()->posts()->create($validatedData);

答案 1 :(得分:0)

您可以通过两种类型进行求解。

  1. 您可以在数据库NULL中设置null值(是) enter image description here

  2. 使用name =“ title”