我有一个名为 的模型CroppedDocumentField,其$casts设置如下:
protected $casts = [
'content' => 'array'
];
迁移如下所示:
Schema::create('cropped_document_fields', function(Blueprint $table){
$table->increments('id');
$table->unsignedInteger('document_id');
$table->json('content')->nullable();
});
在我的数据库中,内容列似乎像字符串一样存储:
"{\"1\": [{\"row\": \"Bill Tc\\n\"}, {\"row\": \"Nathar\\n\"}, {\"row\": \"75839\\n\"}]}"
如果我回应:
$document = CroppedDocumentField::Find(56);
dd(is_array($document->content));
这将返回 false。
当我将 JSON 插入到我的数据库时,我从包含 JSON 字符串的 .txt 文件中读取它:
{"1": [{"row": "Bill Tc\n"}, {"row": "Nathar\n"}, {"row": "75839\n"}]}
然后我插入它:
$file = "mytext.txt";
$content = file_get_contents($file);
//Add the text content
$this->document->cropped()->create([
'content' => $content
]);
在我的文档模型中,我只是与CroppedDocumentField模型有关系:
//Document.php:
public function cropped()
{
return $this->hasMany(CroppedDocumentField::class);
}
我在这里做错了什么?