Update Fields Using Checkbox - Laravel

Asked

Viewed 157 times

4

I have a User Registration form, where I make available the options to select several options in Checkbox.

Registration - Sports Practice: Football, Volleyball, Swimming and etc. (Users can select several).

I’m having trouble updating records, because I need to show the fields that can be filled in the update and mark the previously chosen ones.

In the section below I only display the available items. I need to pick up the marked entry and mark as checked.

View

           @foreach ($esportesas $item)
              <label class="col-md-12 col-xs-12">
               <input name="areas[]" value="{{ $item->id }}" type="checkbox">{{
                     ($item->area)
                   }}
               </label>
               @endforeach
              </label>
            @endforeach  

Controller

 public function edit($id)
 {
     /* Dados Cadastrados */
     /* Aqui utilizo um relacionamento via Eloquent utilizo Foreach para fazer a Interação Ex: 

     foreach ($dados->esportes as $value) {
        var_dump($value->id)
     }

     */
    $dados = Usuarios::find($id);



     /* Lista de Esportes Cadastrados para Gerar View */
     $esportes = Esportes::all();

     return view('/edit', compact('dados', 'esportes'));

  }
  • Are you running that @foreach? For he has 2 @endforeach, this is related to another looping?

1 answer

1


The easiest way I think would be to use the Laravel Collective (however it is being reshaped temporarily is out)

But we go in the most manual form:

    @foreach ($esportesas $item)
    <label class="col-md-12 col-xs-12">
        @if (in_array($item->id, $arrayJaSalvoNoBanco))
            <input name="areas[]" value="{{ $item->id }}" type="checkbox" checked>
        @else
            <input name="areas[]" value="{{ $item->id }}" type="checkbox" >
        @endif
        {{($item->area)}}
    </label>
    @endforeach
</label>
@endforeach  

Here I am using the in_array(); to check whether the id of the loop of foreach is contained in the array $arrayJaSalvoNoBanco.

Where the $arrayJaSalvoNoBanco would be an array containing the id’s that were already saved in the database for this update in question.

OBS.: The $arrayJaSalvoNoBanco is just one example.

Test there and see if it works.

  • Hello Bulfaitelo, So in Array $sports I have only the values like: Football, Volley and etc. I could not find a way to play the option chosen in the register for this screen. I was able to mark the checkbox using another foreach, but the fields are duplicated.

  • In the controller that you call this view, you probably pass an id, do the search based on that id and take the items that it has already saved returns inside the view and basically this is it.

  • So even it works, I can get the values that are saved in the bank. But I couldn’t find a way to put them in this Foreach.

  • 1

    Update the question showing me how you take this data and obtained result, so I understand better your environment

  • I updated the question, put the Controller. On the previous topic is only a foreach. I was doing some tests.

  • Bulgaitelo, I’ve updated the question.

Show 1 more comment

Browser other questions tagged

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