模拟我创建,以填补行数据库不明白,它需要将其存储到数据库之前将数据转换成JSON。出于某种原因,当从模拟数据插入到它跳过铸造JSON数据库。因此,当逻辑然后从数据库中读取行返回该错误。
json_decode() expects parameter 1 to be string, array given
如果我把它作为模拟的数组。当我们在控制器解码就会产生这个错误。如果我在模拟编码,它返回相同的错误。如果我编码它在模拟和模型中移去投,它按预期工作。
有谁知道如何正确地做到这一点?
这是我的模拟
class SettingMocks
{
public static function getSetting() {
return [
'id' => 1,
'name' => 'Standard',
'settings' => json_encode([
'objective' => "CONVERSIONS",
'gender' => [1,2],
])
];
}
}
这是我的模型
class Setting extends Model
{
protected $guarded = [];
protected $casts = [
'settings' => 'json',
];
}
也许在你的模型像这样mutators and accessor:
public function getSettingsAttribute($value)
{
return json_decode($value);
}
public function setSettingsAttribute($value)
{
$this->attributes['settings'] = json_encode($value);
}