首页 > 解决方案 > 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();

标签: phpmysqllaravel-6

解决方案


解决此问题的最简单方法是'strict' => false,在您的config/database.php文件中设置。另一种方法是添加$table->integer('invest_id')->unsigned()->nullable();以通知数据库该值可以为空。默认'strict' => true,选项在过去给我带来了一些问题,因为它是在 Laravel 5.2 中引入的。

如果您运行一个全新的应用程序,您可以选择严格选项,但您必须相应地设置您的数据库。


推荐阅读