I have a database table with 2 columns, an auto incrementing ID and a name. I am trying to add checkboxes to my form for each ID from this table. I'm trying to do this using a foreach, but I am not sure if this is the correct way, I'm very new to laravel.
This is in my controller:
$role = DB::table('role')->lists('role_name', 'id');
$labourTypes = DB::table('labour_type')->lists('labour_type_name', 'id');
return view('pages.addemployee', compact('role', 'labourTypes'));
This is in my forms page .php
@foreach($labourTypes as $labourType)
<div class="checkbox">
<label>
@if(in_array($labourType->id))
{{ Form::checkbox('labourType[]', $labourType->id, false) }}
@endif
{{ $labourType->labour_type_name }}
</label>
</div>
@endforeach
I'm just trying to populate the form with a label (labour_type_name), and a corresponding checkbox.
Thanks.
The code sample is missing the second arg for in_array, it doesn't know where to look
Sorry, if I am understanding this correctly, it should be
@if(in_array($labourType->id, $labourTypes))
Still receiving an error.
Actually if your just trying to list out each element you should be able to use:
@foreach($labourTypes as $id => $name)
<div class="checkbox">
<label>
{!! Form::checkbox("labour_types[]", $id) !!} {{$name}}
</label>
<div>
@endforeach
Notice I switched the field name to snake_case, html doesn't recognize capitalization
I can suggest you to try using "Cloudbacko Software" as this software gives best results while taking backup and with backup it can help you in restoring also. Try it!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community