【问题标题】:Laravel integrity constraint violation when trying to use save尝试使用保存时违反 Laravel 完整性约束
【发布时间】:2014-12-18 16:32:05
【问题描述】:

我收到一个错误:

完整性约束违规:19 NOT NULL 约束失败:songs.slug (SQL: insert into "songs" ("title", "updated_at", "created_at") 值(只要你爱我测试,2014-10 -22 04:09:02, 2014-10-22 04:09:02))

尝试更新歌曲时。有人知道我做错了什么吗?

这是我的更新功能:

public function update(Song $song, Request $request)
{
   $song->fill(['title' => $request->get('title')])->save();
   return redirect('songs');
}

谁能帮我理解这里出了什么问题?我正在跟随Laracast 构建这个函数,它似乎应该工作它只是给我这个约束错误。请帮忙:)

【问题讨论】:

    标签: laravel eloquent


    【解决方案1】:

    确保你在 Song 模型中设置了 protected $fillable = ['title'];,因为 Laravel 可以防止批量赋值(使用 ->fill()),这意味着它会忽略你传递给填充方法的任何键/值,而不是之前在模型的 $fillable 属性中定义!

    【讨论】:

    • 是的,我确实已经处理好了。还有其他想法吗?
    【解决方案2】:

    正如这个错误所说的

    Integrity constraint violation: 19 NOT NULL constraint failed: songs.slug (SQL: insert into "songs" ("title", "updated_at", "created_at") values (As Long As You Love Me test, 2014-10-22 04:09:02, 2014-10-22 04:09:02))
    

    您的songs 表的slug 列是NOT NULL,您在保存模型之前尚未为其设置值。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 2017-09-11
    相关资源
    最近更新 更多