php - SQLSTATE [23000]:违反完整性约束:1048 列“invest_id”不能为空
问题描述
全部
请帮助解决上述错误。
我正在尝试将数据发布到数据库。
这是下表:
public function up() {
Schema::create('investment', function (Blueprint $table) {
$table->bigIncrements('id');
$table->decimal('amount');
$table->integer(**'invest_id')->unsigned()**;
$table->integer('reference_id')->unsigned();`
这是控制器上的代码
public function confirm(Request $request)
{
$this->validate($request, [
'amount' => 'required|numeric',
'plan_id' => 'required|numeric',
]);
$interest = new Interest();
$interest->invest_id = $request->invest_id;
$interest->user_id = $user->id;
$interest->save();
解决方案
解决此问题的最简单方法是'strict' => false,
在您的config/database.php
文件中设置。另一种方法是添加$table->integer('invest_id')->unsigned()->nullable();
以通知数据库该值可以为空。默认'strict' => true,
选项在过去给我带来了一些问题,因为它是在 Laravel 5.2 中引入的。
如果您运行一个全新的应用程序,您可以选择严格选项,但您必须相应地设置您的数据库。
推荐阅读
- java - 找不到存在的元素,得到 NullPointerException 而不是 NoSuchElement
- javascript - RxJS takeUntil 不会取消订阅
- java - Java 递归内存
- c++ - 在结构中设置数组的值
- c++ - 使用 C++ 在多线程应用程序中测量全局时间(挂钟)的最快方法
- python - 汇总来自不同数据框的数据框列
- python - 使用 Python 将嵌套的 JSON 导出为 CSV
- java - Spring不接受环境变量
- mysql - 为类似 Tinder 的应用程序设计的高效 MySQL 数据库
- node.js - 必须根据匹配的 req.params.id 删除对象的 ID