【发布时间】:2018-06-10 05:33:34
【问题描述】:
我正在尝试将带有选项的数组保存到我的 postgres 数据库的 json 数据字段中。我正在使用 Laravel 5.5,并且正在使用扩展名“dimsav/laravel-translatable”进行翻译。
我的模型问题如下所示: 命名空间应用程序;
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'];
}
以及 QuestionsController 中的 store 操作:
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 铸造不起作用?也许是因为可翻译的扩展名?
【问题讨论】:
-
我在这里得到了帮助:laracasts.com/discuss/channels/laravel/…
标签: php postgresql laravel-5.5