Reputation: 1092
I have a project. At the moment, I am working with a CV (curriculum vitae) table. So, I was at the point that each CV can contain one or more Phone numbers and e-mails.
Because I already had around 7 tables associated with CV like skills, languages and so on, I decided to save emails and phones in the Сv s columns, as JSON columns.
So, in my migration I put
$table->json('phone');
$table->json('email');
How could I manipulate this
<input type="text" name="phone[]">
<input type="text" name="email[]">
for storing this data from Controller in DB, and how should I retrieve them in view?
Upvotes: 3
Views: 3005
Reputation: 163748
Use attribute casting. In this case, you'll not need to convert the data from array to JSON and back.
From the docs:
The array cast type is particularly useful when working with columns that are stored as serialized JSON. For example, if your database has a JSON or TEXT field type that contains serialized JSON, adding the array cast to that attribute will automatically deserialize the attribute to a PHP array when you access it on your Eloquent model
protected $casts = [
'phone' => 'array',
'email' => 'array',
];
Upvotes: 2