I
I
Ivan2020-08-20 15:46:50
Laravel
Ivan, 2020-08-20 15:46:50

How to add data to a table field, adding to what is already there?

Through the model I write data to the table. Rather, I update one field. The array is stored in a string field. And so, how can you update this field by adding another array to this array? It does not matter whether the same or different values ​​\u200b\u200bwill come, before what is there to add.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton October, 2020-08-20
@Djonson86

As I understand it, if you save an array in the string field in the database, then you most likely encode this array into JSON to write it as a string. Right?
As an option:

$array = json_decode($model->field); // декодируем string в array
$data = ['массив которым хотим дополнить', 'поле'];
$array = array_merge($array, $data); // объединяем массивы
$model->field = json_encode($array); // кодируем и сохраняем
$model->save();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question