SQLSTATE[23000]:违反完整性约束:19 NOT NULL 约束失败:posts.user_id
SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: posts.user_id
我正在尝试创建一个创建页面,在该页面中,用户以文本形式输入标题并选择图片,但是当我单击按钮添加新内容时 Post 我收到此错误:
SQLSTATE[23000]:违反完整性约束:19 NOT NULL 约束失败:posts.user_id(SQL:插入“post s" ("caption", "image", "updated_at", "created_at") 值 (asd, C:\xampp\tmp\phpB7DC.tmp, 2021-03-12 13:06:56, 2021-03-12 13:06:56))
我想告诉你我正在使用 sqlite 数据库。
Post的模型:
class Post extends Model
{
protected $fillable = [
'caption', 'image',
];
public function user()
{
return $this->belognsTo(User::class);
}
}
Posts控制器:
class PostsController extends Controller
{
public function create()
{
return view('posts.create');
}
public function store() {
$data = request()->validate([
'caption' => 'required',
'image' => 'required|image',
]);
\App\Post::create($data);
dd(request()->all());
}
}
迁移文件夹中我的tablepost:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('caption');
$table->string('image');
$table->timestamps();
$table->index('user_id');
});
}
您没有在交易中发送 user_id 价值
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id'); // this value must be set
$table->string('caption');
$table->string('image');
$table->timestamps();
$table->index('user_id');
});
}
在这部分
class PostsController extends Controller
{
public function create()
{
return view('posts.create');
}
public function store() {
$data = request()->validate([
'caption' => 'required',
'image' => 'required|image',
]);
// In this section you must add user_id to data
// For example
$data['user_id'] = Auth::user()->id;
\App\Post::create($data);
dd(request()->all());
}
}
在您的迁移中,您没有将此列写为 Nullable。
您需要在验证中添加 post_id
。
$data = request()->validate([
'caption' => 'required',
'image' => 'required|image',
'post_id' => 'required|int'
]);
我正在尝试创建一个创建页面,在该页面中,用户以文本形式输入标题并选择图片,但是当我单击按钮添加新内容时 Post 我收到此错误:
SQLSTATE[23000]:违反完整性约束:19 NOT NULL 约束失败:posts.user_id(SQL:插入“post s" ("caption", "image", "updated_at", "created_at") 值 (asd, C:\xampp\tmp\phpB7DC.tmp, 2021-03-12 13:06:56, 2021-03-12 13:06:56))
我想告诉你我正在使用 sqlite 数据库。
Post的模型:
class Post extends Model
{
protected $fillable = [
'caption', 'image',
];
public function user()
{
return $this->belognsTo(User::class);
}
}
Posts控制器:
class PostsController extends Controller
{
public function create()
{
return view('posts.create');
}
public function store() {
$data = request()->validate([
'caption' => 'required',
'image' => 'required|image',
]);
\App\Post::create($data);
dd(request()->all());
}
}
迁移文件夹中我的tablepost:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('caption');
$table->string('image');
$table->timestamps();
$table->index('user_id');
});
}
您没有在交易中发送 user_id 价值
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id'); // this value must be set
$table->string('caption');
$table->string('image');
$table->timestamps();
$table->index('user_id');
});
}
在这部分
class PostsController extends Controller
{
public function create()
{
return view('posts.create');
}
public function store() {
$data = request()->validate([
'caption' => 'required',
'image' => 'required|image',
]);
// In this section you must add user_id to data
// For example
$data['user_id'] = Auth::user()->id;
\App\Post::create($data);
dd(request()->all());
}
}
在您的迁移中,您没有将此列写为 Nullable。
您需要在验证中添加 post_id
。
$data = request()->validate([
'caption' => 'required',
'image' => 'required|image',
'post_id' => 'required|int'
]);