繁体   English   中英

如何在流明中将默认属性定义为空数组

[英]How to define default attribute as empty array in lumen

目前我正在使用 mongodb 作为数据库,我创建了一个集合,其中每个字段都需要输入数组,如果未提供值,我希望将它们设置为默认空数组。

class Scores extends Eloquent
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'AllScore';

    /**
     * The attributes that should be cast.
     *
     * @var array
     *
     */
    protected $casts = [
        
        'y3_score'   => 'array',
        'y4_score'   => 'array',
        'y5_score'   => 'array',
        'y6_score'   => 'array',
    ];

    /**
     * The model's default values for attributes.
     *
     * @var array
     */
    protected $attributes = [
        'y3_score'   => [],
        'y4_score'   => [],
        'y5_score'   => [],
        'y6_score'   => [],
    ];
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'y3_score',
        'y4_score',
        'y5_score',
        'y6_score',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
    ];
}

但是在发布请求时,为该集合创建数据时会返回错误

(1/1) ErrorException
json_decode() expects parameter 1 to be string, array given

in HasAttributes.php line 715

at Application->Laravel\Lumen\Concerns\{closure}(2, 'json_decode() expects parameter 1 to be string, array given', '/var/www/html/pathway_ws/LumenConnection/vendor/illuminate/database/Eloquent/Concerns/HasAttributes.php', 715, array('value' => array(), 'asObject' => false))

但是当我删除这些属性的默认空数组时,一切正常

我遇到了类似的错误(但 Laravel 与 MySql)决定很简单:

    protected $attributes = [
    'y3_score'   => '[]',
    ...
];

注意 [] 周围的撇号

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM