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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46638534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:46:09  来源:igfitidea点击:

SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed:

phplaravellaravel-5.5

提问by Femi Ogunmokun

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:

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

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

Here is my controller:

这是我的控制器:

<?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:

这是我的任务模型:

namespace App;

命名空间应用程序;

use Illuminate\Database\Eloquent\Model;

使用 Illuminate\Database\Eloquent\Model;

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

}

here is the model for users:

这是用户的模型:

<?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 the view:

这是视图:

@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

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

似乎无法弄清楚问题,谷歌似乎也没有想法。

回答by pallavi nawani

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.

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

To correct it :

要纠正它:

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

1) 为该列提供一个非空值并添加到您的任务模型片段下面:

protected $fillable = [
    'description'];

or

或者

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

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

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

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

从它的任务模型中删除您设置为可空的列名(在您的表中)。

=> Empty the database in phpmyadminby deleting all the tables.

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

=> Run the command : php artisan migrate

=> 运行命令: php artisan migrate

=> Try performing your action again.

=> 再次尝试执行您的操作。

回答by andymcgregor

To your Tasks model, try to add:

在您的 Tasks 模型中,尝试添加:

protected $fillable = ["description", "user_id"];

回答by pvaitonis

You need to refresh your database tables. You have a columnin database which doesn't exist in your Model or migration. and you are not entering data for that field. I just DROPPED all my tables and ran migrations. php artisan migrate:fresh.

您需要刷新数据库表。您有一个column在您的模型或迁移中不存在的数据库。并且您没有为该字段输入数据。我只是删除了所有表并运行了迁移。 php artisan migrate:fresh.

回答by tisuchi

Its because of description values in tasks table is null.

这是因为任务表中的描述值为空。

Here is the way how to fix that-

这是如何解决这个问题的方法 -

  1. If description can be null, than make it nullable from database table structure.

  2. Add $fillableproperty on model-

    class Task extends Model
    {
        //add this
        protected $fillable = ["description", "user_id"];
    
        public function user()
        {
            return $this->belongsTo(User::class);
        }
    }
    
  1. 如果 description 可以为空,则使其从数据库表结构中可以为空。

  2. $fillable在模型上添加属性-

    class Task extends Model
    {
        //add this
        protected $fillable = ["description", "user_id"];
    
        public function user()
        {
            return $this->belongsTo(User::class);
        }
    }