I have a website where users can transform a text document and save it to what I call a field. One text document can have a dynamic amount of fields, thus making it hard for me to create a relational database design.
Consider below setup.
My model Field.php have below fields table.
id | name | type
-----------------------
1 | invoice_no | text
2 | addresses | table
I also have a table where I save the field result for a specific document:
On model Result I have a $casts type:
protected $casts = [
'content' => 'array'
];
This is my results table:
id | field_id | document_id | content
-------------------------
1 | 1 | 32 | #81724
2 | 2 | 32 | [{"0": "Parkway", "1": "Broadway"}, {"0": "Avenue St.", "1": "Main St."}]
The data from the table is simply being read from a .json file.
Now ultimately, when all fields for a specific document (in this case 32) is entered in my database, I would like to send a webhook with the field data, such as:
{
"invoice_no":"#81724",
"addresses": [{
"0": "Parkway",
"1": "Broadway"
},{
"0": "Avenue St.",
"1": "Main St."
}]
}
So above is what I have visioned - but I am not sure if this is the best approach?
- The
$castson my Model returns thecontentas an array - but as you can see forresults.content, it can be both a string and an array. - How should I store the content in my database? I cannot make the column as a
JSONtype, if I also want to store strings.
Another approach?
Would it make more sense to have two columns on the results table for the content, so it becomes:
id | field_id | document_id | text | array
----------------------------------------------
1 | 1 | 32 | #81724 | NULL
2 | 2 | 32 | NULL | [{"0": "Parkway", "1": "Broadway"}, {"0": "Avenue St.", "1": "Main St."}]