SQLSTATE [23000]:完整性约束违规:1048列' job_description'不能无效

发布于 2025-02-13 06:54:27 字数 1164 浏览 1 评论 0原文

sqlState [23000]:完整性约束违规:1048列“ job_description'不能为null(sql:insert in job (job_titlejob_description ,<代码>文件Note)valut(eee,?,user.jpg,kjl))

迁移

    {
        Schema::create('jobs', function (Blueprint $table) {
            $table->id();
              $table->String('job_title');
                $table->String('job_description');
                  $table->String('file');
                    $table->String('note');
            $table->timestamps();
        });
    }  

模型

{
    use HasFactory;
    protected $fillable = [

        'job_title',
        'job_description',
        'file',
        'note',
    ];

控制器

      Job::insert([
        'job_title'=>$request->job_title,
        'job_description'=>$request->job_description,
        'file'=>$request->file,
        'note'=>$request->note
      ]);
      return Redirect()->back()->with('success','Job added successfully');
    }

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'job_description' cannot be null (SQL: insert into jobs (job_title, job_description, file, note) values (EEE, ?, user.JPG, KJL))

migration

    {
        Schema::create('jobs', function (Blueprint $table) {
            $table->id();
              $table->String('job_title');
                $table->String('job_description');
                  $table->String('file');
                    $table->String('note');
            $table->timestamps();
        });
    }  

model

{
    use HasFactory;
    protected $fillable = [

        'job_title',
        'job_description',
        'file',
        'note',
    ];

controller

      Job::insert([
        'job_title'=>$request->job_title,
        'job_description'=>$request->job_description,
        'file'=>$request->file,
        'note'=>$request->note
      ]);
      return Redirect()->back()->with('success','Job added successfully');
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稀香 2025-02-20 06:54:27

数据库中的列Job_description设置为非零约束。以及尝试使用$ request-&gt; job_description设置的值。这是这个问题的根本原因。

您可以修改控制器。

Job::insert([
        'job_title'=>$request->job_title,
        'job_description'=>(!is_null($request->job_description) ? $request->job_description : ""),
        'file'=>$request->file,
        'note'=>$request->note
      ]);

为了纠正您可以使用Input()为字段提供默认值,

Job::insert([
    'job_title' => $request->job_title,
    'job_description' => $request->input('job_description', ''),
    'file' => $request->file,
    'note' => $request->note
]);

The column job_description in the database is set up as NOT NULL constraints. And the value trying to set using $request->job_description with NULL. That is the root cause of this issue.

To rectify you can modify your controller like:

Job::insert([
        'job_title'=>$request->job_title,
        'job_description'=>(!is_null($request->job_description) ? $request->job_description : ""),
        'file'=>$request->file,
        'note'=>$request->note
      ]);

Or provide a default value for fields using input()

Job::insert([
    'job_title' => $request->job_title,
    'job_description' => $request->input('job_description', ''),
    'file' => $request->file,
    'note' => $request->note
]);
听闻余生 2025-02-20 06:54:27

使用nullable用于列声明$ table-&gt; string('job_description') - &gt; nullable(),如果您想将其与null值一起使用。

Use nullable for column declaration $table->string('job_description')->nullable(), if you want use it with null value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文