Laravel 5.4 字段没有默认值

Laravel 5.4 field doesn#39;t have a default value(Laravel 5.4 字段没有默认值)
本文介绍了Laravel 5.4 字段没有默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我遇到了这个错误,我检查过的谷歌搜索结果都与我的问题不相似.

I am having this error and none of the googled result i checked is similar to my problem.

我有一个包含 Deal、User 和 Matches 类的应用程序

I have an application with class Deal, User, and Matches

一笔交易有很多匹配项.一个用户有很多匹配.一个用户有很多交易.

A deal has many matches. A user has many matches. A user has many deals.

我正在尝试使用我的 Deal 对象创建一个新的匹配

I am attempting to create a new Match using my Deal object

$deal->matches()->create(['user_id'=>$id]);

这是我的匹配类,我已经定义了所有需要的关系

This is my match class, i have defined all needed relationships

class Match extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];
    public $timestamps = false;
    public $expired_on = "";


    public static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            $model->matched_on = $model->freshTimestamp();
        });
    }

    public function __construct(){
        $d = (new DateTime($this->matched_on))->modify('+1 day');
        $this->expired_on = $d->format('Y-m-d H:i:s');
    }


    /**
     * Get the user that owns the match.
     */
    public function user()
    {
        return $this->belongsTo('AppUser');
    }

    /**
     * Get the deal that owns the match.
     */
    public function deal()
    {
        return $this->belongsTo('AppDeal');
    }
}

当我尝试创建新匹配项时,我不断收到此错误.

And i keep getting this error when i attempt to create a new match.

Connection.php 第 647 行中的 QueryException:SQLSTATE[HY000]:一般错误:1364 字段user_id"没有默认值(SQL:插入matches(deal_id)值(1))

QueryException in Connection.php line 647: SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into matches (deal_id) values (1))

我的守卫是一个空数组,可能是什么问题?

I have my guarded to be an empty array, what could be the problem?

推荐答案

移除 guarded 数组并添加 fillable 代替:

Remove the guarded array and add the fillable instead:

protected $fillable = ['user_id', 'deal_id'];

这篇关于Laravel 5.4 字段没有默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)