本文介绍了SQLSTATE [23000]:违反完整性约束:19 NOT NULL约束失败:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在laravel上创建待办事项列表应用程序,但是当我尝试单击按钮时,创建新的待办事项列表时出现此错误:

Trying to create a to-do list app on laravel but when i try to click on the button the create a new to-do list i get this error:

SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: tasks.description (SQL: insert into "tasks" ("description", "user_id", "updated_at", "created_at") values (, 1, 2017-10-09 03:28:35, 2017-10-09 03:28:35))

这是我的控制人:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */

    public function tasks()
    {
        return $this->hasMany(Task::class);
    }

    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

这是我执行任务的模型:

Here is my model for tasks:

命名空间应用;

使用Illuminate \ Database \ Eloquent \ Model;

use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }

}

这是用户模型:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */

    public function tasks()
    {
        return $this->hasMany(Task::class);
    }

    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

这是视图:

@extends('layouts.app')

@section('content')
    <div class="container">
        @if (Auth::check())
            <h2>Tasks List</h2>
            <a href="/task" class="btn btn-primary">Add new Task</a>
            <table class="table">
                <thead><tr>
                    <th colspan="2">Tasks</th>
                </tr>
                </thead>
                <tbody>@foreach($user->tasks as $task)
                    <tr>
                        <td>
                            {{$task->description}}
                        </td>
                        <td>

                            <form action="/task/{{$task->id}}">
                                <button type="submit" name="edit" class="btn btn-primary">Edit</button>
                                <button type="submit" name="delete" formmethod="POST" class="btn btn-danger">Delete</button>
                                {{ csrf_field() }}
                            </form>
                        </td>
                    </tr>


                @endforeach</tbody>
            </table>
        @else
            <h3>You need to log in. <a href="/login">Click here to login</a></h3>
        @endif

    </div>
@endsection

似乎无法解决问题,而Google似乎也没有任何想法.

Can't seem to figure out the problem and google seems not to have an idea either.

推荐答案

所收到的错误意味着表中有些列没有null约束,并且您正在插入/更新null值.

The error you are getting means that there are some columns in the table which have not null constraint and you are inserting/updating null value.

要纠正它:

1)在该列中提供一个非null值,然后将其添加到下面的任务模型代码段中:

1) Provide a not null value to the column and add to your tasks model snippet below:

protected $fillable = [
    'description'];

2)通过写入以下内容,使迁移文件中的表中的列为空:

2) Make the column nullable in the table in your migration file by writing:

$table->('description')->nullable();

从其任务模型中删除您使表中可以为空的列名.

Remove the column name which you made nullable (in your table) from it's tasks model.

=>通过删除所有表来清空 phpmyadmin 中的数据库.

=> Empty the database in phpmyadmin by deleting all the tables.

=>运行命令: php artisan migration

=>尝试再次执行操作.

=> Try performing your action again.

这篇关于SQLSTATE [23000]:违反完整性约束:19 NOT NULL约束失败:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 04:10