gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:27:06 31 4
gpt4 key购买 nike

尝试在 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))

这是我的 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',
];
}

这是我的任务模型:

命名空间应用;

使用 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',
];
}

这是 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

似乎无法找出问题,谷歌似乎也没有想法。

最佳答案

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

更正它:

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

protected $fillable = [
'description'];

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

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

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

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

=> 运行命令:php artisan migrate

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

关于php - SQLSTATE[23000] : Integrity constraint violation: 19 NOT NULL constraint failed:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46638534/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com