[英]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.