Error: Array to string Conversion - Select Multiple Laravel 5

Asked

Viewed 378 times

1

In my View I have:

<select multiple="multiple" name="carga_mental[]" class="form-control select2">
    @foreach($cadastros->where('key', 'carga_mental') as $carga_mental)
        <option value="{{ $carga_mental->title }}"> {{ $carga_mental->title }} </option>
    @endforeach
</select>

In my Controller I have:

$this->repository->update($request->all(), $id);

When I submit the form returns this error:

Array to string Conversion (SQL: update relatorio_setores set carga_mental = Comfortable Conditions, Where id = 4)

And he only takes the first selected item.

My intention is to record the selected items in the table in JSON format, how can I do this?

  • your bank accepts json native, ie, is already the version of Mysql that has a field json?

  • I can’t tell @Virgilionovic, I thought it was just playing on a field like text

  • There is a way to work if I want to post a proposal! but, also $this->repository that’s screwing up a little bit what’s inside that

1 answer

0

In the way you sent directly, without converting the data will give error even, need to convert with json_encode, to send a text in the format for the table in the bank. In MySQL to from version 5.7 onwards there is already a field responsible for saving data in the format and the used in the Laravel already does it. But apparently you need to do it the traditional way by writing to a table in some field varchar.

What it would be like basically by your code:

// pegando todos menos carga_mental
$data = $request->except('carga_mental'); 

//criando a chave carga_mental com um texto no formato json
$data['carga_mental'] = json_encode($request->input('carga_mental'));

//atualizando ...
$this->repository->update($data, $id);

that is, the conversion must be done before sending to the routine/method that record or updates your information.

Just remember that to use this field in the format array needs to be converted with json_decode.

References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.