我遇到此错误,我检查过的所有googled结果均与我的问题相似。

我有一个类别为Deal,User和Matchs的应用程序

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

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

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

这是我的比赛类,我已经定义了所有需要的关系
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('App\User');
    }

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

当我尝试创建新比赛时,我一直收到此错误。



我谨防成为一个空数组,这可能是什么问题?

最佳答案

删除guarded数组,然后添加fillable:

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

关于php - Laravel 5.4字段没有默认值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43186521/

10-09 08:13