Luke Sweeney Luke Sweeney - 7 months ago 32
PHP Question

Laravel foreach loop not outputting to form

Here is a form from my Laravel 5 project:

<form class="form-group" action="/counselors/{{ $counselor->id }}/badges/add" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
@foreach($badges as $badge)
<div class="input-group">
<span class="input-group-addon">
<input type="checkbox" style="float: left" value="{{ $badge->id }}">&nbsp;{{ $badge->name }}
</span>
</div>
@endforeach
<br>
<input type="submit" class="form-control btn btn-primary" name="submit" value="Submit"><hr>
<input type="button" class="form-control btn btn-danger" name="cancel" value="Cancel" onClick="location='/counselors'">
</form>


The $badges is a collection of badges from a database. They all iterate and display properly, but when i look at the request data with

$request->all()


it returns:

{"_token":"5E4csIJU4YVVQZSoPG1EmpfHfjNjYcRwuOoreCcE","submit":"Submit"}


It doesn't include the checkbox data from the loop. Is this incorrect? What's the best way to do this? There are 160 items in the $badges collection.

I'm pretty new so please go easy.

Answer

put name attribute to the input checkbox and giv it a value

<input type="checkbox" name="check-{{ $badge->id }}" style="float: left" value="{{ $badge->id }}">&nbsp;{{ $badge->name }}

Note : here i use the name "check-" and the id of the badge to get defferent name for each checkbox