I am wondering what the best way to accomplish this would be. I need to store an array such as:
['hello', 'world', 'love']
Inside one column in a table. I do not want to create a new table where each value in the array becomes a separate row. Is there a better way? I guess my biggest concern is performance.
So when creating the migration for this column, ideally I would insert an array with 3 default/nullable values. Then I can set actual values for the array when needed in my application.
Simple scenario: I have a table called desk
and I want to store 3 items that are on top of the desk such as ['pencil', 'ruler', 'stapler']
, but I do not want to create a separate table desk_items
to have a row for each item. Hope this makes sense.
Using Laravel and MYSQL
Thank you.
CodePudding user response:
make desc table as jsonb format
$table->jsonb('desk');
and in model use casts
protected $casts = [
'desk' => 'array',
];
CodePudding user response:
If you want to store the array in Database table you should send it with json_encode
.
$data = new Data;
$data->Name = json_encode($request->DataName);
$data->save();
Thus, you can store an array in the name column of your data table.
to use this data you need to call the data with json_decode
.
$data = json_decode(Data::find(1)->Name);