Laravel 5.3创建模型返回“字段没有默认值" [英] Laravel 5.3 Creating Models Returns "Field doesn't have a default value"
问题描述
我使用Laravel和Eloquent已有两年了,今天我决定安装一个新的Laravel 5.3并尝试使用它.
I'm using Laravel and Eloquent for two years and today I've decided to install a fresh Laravel 5.3 and try something with it.
我使用了我的旧数据库架构并创建了我的模型,定义了可填充列.这是我的Page
模型的样子:
I used an old database schema of mine and created my models, defined fillable columns. This is what my Page
model looks like:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Page extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'language',
'title',
'slug',
'url',
'description',
'tags',
'wireframe',
'order',
'is_active'
];
public function menus()
{
return $this->belongsToMany(Menu::class);
}
}
url
属性是MySQL上的TEXT
类型列,因此,如果我在创建模型时不将任何值传递给它,则它应该为空字符串.相反,我不断收到SQLSTATE[HY000]: General error: 1364 Field 'url' doesn't have a default value
错误.
url
attribute is a TEXT
-type column on MySQL so if I don't pass any value to it when creating a model, it should be a empty string. Instead, I keep getting SQLSTATE[HY000]: General error: 1364 Field 'url' doesn't have a default value
error.
这是我尝试创建帖子模型的尝试:
Here is my attempt to create a Post model:
Page::create([
'title' => $root_menu['title'],
'slug' => $root_menu['slug'],
'language' => $this->language,
'wireframe' => key(config('cms.wireframe')),
'order' => 0
]);
这是与Laravel 5.3相关的问题吗?预先感谢您的帮助.
Is this a Laravel 5.3 related issue or am I missing something? Thanks in advance for your helps.
推荐答案
@Nicklas已解释了错误的原因.
The reason for the error has been explained by @Nicklas.
但是,现在发生这种情况的原因是,默认情况下,Laravel 5.3对MySQL使用strict
模式.
The reason this is happening now, however, is that Laravel 5.3 uses strict
mode for MySQL by default.
如果您想恢复以前的行为,请更新您的config/database.php
文件并为连接设置'strict' => false
.
If you would like to revert to previous behavior, update your config/database.php
file and set 'strict' => false
for your connection.
这篇关于Laravel 5.3创建模型返回“字段没有默认值"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!