Error: Array to string conversion - Select multiple Laravel 5

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 it returns this error:

  

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

And it only takes the first selected item.

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

    
asked by anonymous 22.11.2016 / 18:42

1 answer

0

In the way you have submitted directly, without converting the data will give you the same error, you must convert with json_encode to send text in to the table in the bank. In%% from from version 5.7 there is already a field responsible for writing data to format and the post-tag" title="show questions tagged 'eloquent'"> eloquent used in MySQL already does this. But apparently you have to do it the traditional way by writing to a table in some field Laravel .

As it would be basically because of 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);

In other words, the conversion must be done before sending to the routine / method that saves or updates your information.

>

Just remembering that to use this field in varchar format needs to be converted with json_decode .

References:

23.11.2016 / 15:27