How to resolve "Array to string Conversion" Laravel Error

Asked

Viewed 702 times

-1

I am trying to update a registration form modal. The following error is occurring:

Array to string Conversion (SQL: update checklist_protocolo set item = 0, item_descricao_id = 5, sim_nao = on, nao_atende = on, dt_validade = 2019-04-29, pagina_documento = 1, observacao = Where Annotation projeto_id = 4)

inserir a descrição da imagem aqui

Follows excerpt from the update code:

$projeto = $request->projeto_id;
$dados = $request->only(['item', 'item_descricao_id', 'sim_nao', 
                         'nao_atende', 'dt_validade', 
                         'pagina_documento', 'observacao']);                               
$checklistsProtocolos = checklistProtocolo::where('projeto_id', $projeto)
->update([
          'item' => $dados['item'],
          'item_descricao_id' => $dados['item_descricao_id'],
          'sim_nao' => $dados['sim_nao'],
          'nao_atende' => $dados['nao_atende'],
          'dt_validade' => $dados['dt_validade'],
          'pagina_documento' => $dados['pagina_documento'],
          'observacao' => $dados['observacao'] ]);

Which is the best way to update this data in the mentioned error?

Follow the table with their respective types:

inserir a descrição da imagem aqui

Registration form:

inserir a descrição da imagem aqui

array(9) { ["projeto_id"]=> string(1) "4" ["modelo_id"]=> string(1) "6" ["item"]=> array(5) { [0]=> string(1) "0" [1]=> string(1) "1" [2]=> string(1) "2" [3]=> string(1) "3" [4]=> string(1) "4" } ["item_descricao_id"]=> array(5) { [0]=> string(1) "5" [1]=> string(1) "6" [2]=> string(1) "7" [3]=> string(1) "8" [4]=> string(1) "9" } ["sim_nao"]=> array(3) { [0]=> string(2) "on" [1]=> string(2) "on" [2]=> string(2) "on" } ["dt_validade"]=> array(5) { [0]=> string(10) "2019-04-30" [1]=> string(10) "2019-04-30" [2]=> string(10) "2019-04-30" [3]=> string(10) "2019-04-30" [4]=> string(10) "2019-04-30" } ["pagina_documento"]=> array(5) { [0]=> string(1) "1" [1]=> string(1) "1" [2]=> string(1) "1" [3]=> string(1) "1" [4]=> string(1) "1" } ["observacao"]=> array(5) { [0]=> string(10) "Anotação" [1]=> string(10) "Anotação" [2]=> string(10) "Anotação" [3]=> string(10) "Anotação" [4]=> string(10) "Anotação" } ["nao_atende"]=> array(2) { [0]=> string(2) "on" [1]=> string(2) "on" } }

Html code:

 <!--Inicio do modal de Checklist do Projeto-->

              {{ csrf_field() }}  

            <div class="modal-dialog2 modal-center">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">×</span>
                        </button>
                        <h4 class="modal-title">Checklists do Protocolo</h4>
                    </div>

                    <div class="form-group col-md-4">
                         <input type="hidden" id="projeto_id" name="projeto_id" value="{{$projeto->id}}">
                         <input type="hidden" id="modelo_id"  name="modelo_id" value="{{$modeloProtocolo[0]['id']}}">
                         <label class="control-label">Modelo</label>
                         <input type="text" class="form-control" name="modeloProcesso" value="{{$modeloProtocolo[0]['modelo']}}" disabled> 

                    </div>
                    <div class="modal-body">
                    <div class="form-group col-md-18">

                         <table id="checklistProtocolo" name="checklistProtocolo" class="table table-hover table-striped table-responsive toggle-arrow-tiny" >
                            <caption></caption>
                                <thead>
                                    <tr>
                                        <th>Item</th> 
                                        <th>Descrição</th>
                                        <th>Sim/Não</th>
                                        <th>Não Atende</th>
                                        <th>Data de Validade</th>
                                        <th>Página do Documento</th>
                                        <th><center>Observações</center> </th>
                                        <th class="text-center"></th>
                                    </tr>
                                </thead>
                                <tbody id="bodyChecklists">

                                @foreach($checklistsProtocolos as $checklistProtocolo)

                                <tr>
                                    <td><input type="text" class="form-control"  id="item" name="item[]" value="{{$checklistProtocolo->item}}"  size ="2"></td>
                                    <td>{{$checklistProtocolo->descricao_item}}</td>   
                                    <input type="hidden"          id="item_descricao_id"  name="item_descricao_id[]" value="{{$checklistProtocolo->item_descricao_id}}">
                                    <td><input type="checkbox"    id="sim_nao"            name="sim_nao[]"    {{$checklistProtocolo->sim_nao == null ? '' : 'checked'}}></td>
                                    <td><input type="checkbox"    id="nao_atende"         name="nao_atende[]" {{$checklistProtocolo->nao_atende == null ? '' : 'checked'}}></td>
                                    <td><input type="date"        id="dt_validade"        name="dt_validade[]" value="{{$checklistProtocolo->dt_validade}}"></td>
                                    <td><input type="text"        id="pagina_documento"   name="pagina_documento[]" value="{{$checklistProtocolo->pagina_documento}}" size ="1"></td>
                                    <td><input type="text"        id="observacao"         name="observacao[]" value="{{$checklistProtocolo->observacao}}" size ="1" style="width: 300px; height: 60px"></td>
                                </tr>

                                @endforeach 

                           </tbody>
                    </table>                              


                    </div>

                    </div><!--Fim do modal-body-->

Printing data without processing fields:

inserir a descrição da imagem aqui

  • could pass this table and its types.

  • @Virgilionovic the description has been updated.

  • saw here oh: nao_atende = on this is a field with only 1 character ??? understood the problem

  • If it takes time to interact the question is as not understood

  • 1

    I updated the description. Note that I need to save an array of data. And persistent error message only shows one line update.

  • gives a command var_dump thus var_dump($dados), because what you’re sending is an array of information ... and glue on the question

  • @Virgilionovic I updated the description with the screen print.

  • Let’s see if it’s a list of values, and so the fields sim_nao and nao_atende does not have the same amount of items as the others, because they are checkbox and PHP does not recover those that do not form marked? If you can fix this.? how is your HTML?

  • Yes. The big question is, without handling these fields (sim_no or nao_atende in the controller will show the "on" values as described in the description. Insert html into description.

  • The question is even worse, those who are not selected do not appear, type if you selected the last one it shows how first and missing 5 checkbox to know whether or not it was selected! the correct there is with select! Just to ratify I know how to solve and it’s not like that! with checkbox gets complicated to solve

  • They will not appear because I used the suggestion, but without that processing of the fields the printing of the data will appear like this: sim_nao = "on" or unanswered = "on" depending on the checkbox marking.

  • is a sequence of values the first line if you do not mark example sim_nao as nothing comes you lose this value , and then that’s the problem you select in the second line and then the value was as if it was the first line ... understood my dear?

  • do the following as a test, swap checkbox for a select with two options and then you’ll understand what I’m talking about

  • I understood what you said only reported what is happening to the field. I will make the suggested test.

Show 9 more comments

1 answer

0


The solution applied was to create a variable ($cont) to count line of each array within the loop of repetition in the registerChecklistProtocolo method.

Reference: Save items from a Checklist form in the database - Laravel

Follows code:

$cont = 0; //contador de linhas para os arrays

              for($i = 0; $i < count($dt_validade); $i++){

                   $dataInsert = array(
                         'sim_nao' => $sim_nao[$cont],                   
                         'dt_validade'=> $dt_validade[$cont], 
                         'pagina_documento' =>$pagina_documento[$cont], 
                         'observacao' =>$observacao[$cont]);           

                         $checklistsProtocolos = checklistProtocolo::where('projeto_id', $projeto_id)
                         ->whereIn('item_descricao_id', (array)$item_descricao_id[$cont])  //atualiza linha por linha do array
                         ->update($dataInsert);
                         $cont++; //atualiza linha por linha do array
                  } 

Browser other questions tagged

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