Laravel 5.3创建模型返回“字段没有默认值" [英] Laravel 5.3 Creating Models Returns "Field doesn't have a default value"

查看:226
本文介绍了Laravel 5.3创建模型返回“字段没有默认值"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆