1

i wanna use laravel collective for my input form,im using this to input datas for pivot table in laravel eloquent many-to-many,and i wanna use input data using checkbox element(hoby),the problem is idont know why we type at the first parameter of laravelCollective as string and way we have to type as array,anyone can explain me?in theory,thanks in advance for your help

<div class="form-check">
    @if (count($list_hobi)>0)
        @foreach ($list_hobi as $key => $value)
        <div class="checkbox">
            {{Form::checkbox('hobi[]',$key,null)}}
            <label>{{$value}}</label>   
        </div>
        @endforeach    
    @endif
</div>
Eddie
  • 26,593
  • 6
  • 36
  • 58
firmansmoh
  • 25
  • 3

1 Answers1

0

Take for example these checkboxes:

<input type="checkbox" name="food" value="apple" /> 1
<input type="checkbox" name="food" value="pear" /> 2
<input type="checkbox" name="food" value="banana" /> 3

All three have the same name. When I check all three and submit the form and see what has been submitted with dd($request->input()), the output is:

"food" => "banana"

It appears only the last input with the same name is saved, even though I selected all three.

When I instead use food[]:

<input type="checkbox" name="food[]" value="apple" /> 1
<input type="checkbox" name="food[]" value="pear" /> 2
<input type="checkbox" name="food[]" value="banana" /> 3

the output is:

"food" => array:3 [▼
    0 => "apple"
    1 => "pear"
    2 => "banana"
]
JorisJ1
  • 922
  • 2
  • 12
  • 22
  • thank a lot it s very kind of you,I know what you are talking about – firmansmoh Aug 12 '19 at 00:02
  • Hi @firmansmoh if this answer has solved your question please consider [accepting](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – JorisJ1 Aug 12 '19 at 05:50
  • Thanks the suggestion – firmansmoh Aug 12 '19 at 11:12