I have a model called CroppedDocumentField, that have below $casts setup:
protected $casts = [
'content' => 'array'
];
The migration looks like this:
Schema::create('cropped_document_fields', function(Blueprint $table){
$table->increments('id');
$table->unsignedInteger('document_id');
$table->json('content')->nullable();
});
In my database, the content column seems to be stored like a string:
"{\"1\": [{\"row\": \"Bill Tc\\n\"}, {\"row\": \"Nathar\\n\"}, {\"row\": \"75839\\n\"}]}"
If I echo that out:
$document = CroppedDocumentField::Find(56);
dd(is_array($document->content));
This returns false.
When I insert the JSON to my database, I read it from a .txt file, that contains the JSON string:
{"1": [{"row": "Bill Tc\n"}, {"row": "Nathar\n"}, {"row": "75839\n"}]}
Then I insert it:
$file = "mytext.txt";
$content = file_get_contents($file);
//Add the text content
$this->document->cropped()->create([
'content' => $content
]);
In my document model, I simply have a relationship to the CroppedDocumentField model:
//Document.php:
public function cropped()
{
return $this->hasMany(CroppedDocumentField::class);
}
What am I doing wrong here?
JSON- will update my question to include the migration.file_get_contentsreturns String, so that's why the data is stored like that. I havejsoncolumn on my project, casting it to array is straight forward.