Laravel 1048 存储数据时列不能为 NULL

2024-04-20

当我尝试保存新联系人时,我收到1048 Column 'username' cannot be NULL错误。很明显,这个错误的原因是空值username,但是我想让它工作而不将列设置为 NULL 或检查是否usernamePOST 数据为空,然后将其值设置为''.

我见过很多例子,其中列未设置为 NULL 和/或数据未设置为''在保存到数据库之前。

或者也许我错了,我错过了一些东西?

我希望有人能对此发表评论..

$contact = new Contact;
$contact->name      = Input::get('name'); 
$contact->username  = Input::get('username');
$nerd->save();

为输入变量设置默认的非空值。

$contact = new Contact;
$contact->name     = Input::get('name');
$contact->username = Input::get('username', '');
$contact->save();

或者,在更新的 Laravel 版本中:

$contact = new Contact;
$contact->name     = $request->input('name');
$contact->username = $request->input('username', '');
$contact->save();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Laravel 1048 存储数据时列不能为 NULL 的相关文章

随机推荐