Support the ongoing development of Laravel.io →
Database Forms

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.

Last updated 3 years ago.
0

The code sample is missing the second arg for in_array, it doesn't know where to look

0

Sorry, if I am understanding this correctly, it should be

               @if(in_array($labourType->id, $labourTypes))

Still receiving an error.

0

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

Last updated 9 years ago.
0

That did the trick - thank you!

0

Your Welcome :)

0

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!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

artyp artyp Joined 4 Nov 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.