【发布时间】:2018-03-20 04:42:11
【问题描述】:
试图在 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))
这是我的控制器:
<?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',
];
}
这里是视图:
@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
似乎无法找出问题所在,谷歌似乎也不知道。
【问题讨论】:
-
你有一个模型作为控制器?
-
不....是什么让你这么认为?
-
也许您没有为某个字段输入数据。检查您的迁移以确保。
标签: php laravel laravel-5.5