php – Laravel在将数组存储到Json数据库字段时获得“数组到字符串转换”

我正在尝试将带有选项的数组保存到我的postgres数据库的json数据字段中.我正在使用Laravel 5.5,我正在使用扩展名“dimsav / laravel-translatable”进行翻译.

我的模型问题看起来像这样:
命名空间App;

use Illuminate\Database\Eloquent\Model;
use Dimsav\Translatable\Translatable;

class Question extends Model
{
    use Translatable;
    public $translatedAttributes = ['options', 'answer'];

    protected $casts = [
        'options' => 'array'
    ];
}

QuestionTranslation模型如下所示:

namespace App;

use Illuminate\Database\Eloquent\Model;

class QuestionTranslation extends Model
{

public $timestamps = false;
public $fillable = ['options', 'answer'];

}

而QuestionController中的商店动作:

 public function store(Request $request)
{
    $question = new Question();

    $options[1] = "test1";
    $options[2] = "test2";

    $question->answer = 1;
    $question->options = $options;

    $question->save();

}

当我尝试存储该数据时,我收到错误

Illuminate \ Database \ QueryException
Array to string conversion (sql: insert into "question_translations" ("locale", "answer", "options", "question_id") values (en, 1, test1, 18) returning "id")

当我使用json_encode自己投射$options时,我可以毫无问题地存储它.

你有什么想法,为什么laravel铸造不工作?也许是因为可翻译的扩展?

解决方法:

在这里得到了帮助:

https://laracasts.com/discuss/channels/laravel/laravel-getting-an-array-to-string-conversion-while-storing-an-array-into-a-json-database-field

使用此解决方案有效

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...