jeudi 28 avril 2016

Undefined offset:1- Laravel Error exception

I Know there are several links referred to this error but still I am unable to find the exact solution of my issue. I have two check-boxes named admin_menu_id[] and admin_sub_menu_id[] whose value i want to insert into database. So far I have done this:-

View page

 @if(is_array($roledatas) && count($roledatas)>0)
    <?php $count = 1;?>
    @foreach($roledatas as $roleKey=>$roleVal)
        <ul id="package_" class="accordion-toggle parentclasstr" data-toggle="collapse" data-parent="#OrderPackages" data-target=".packageDetails">

            <li><input type="checkbox" id="menuChk" value= "<?php echo $id = $roleVal['id']; ?>" name="admin_menu_id[]" /><strong></strong></li>
            <li><i class="indicator glyphicon glyphicon-chevron-up pull-right"></i></li>                        

        @if(isset($roleVal['child']) && is_array($roleVal['child']) && count($roleVal['child'])>0)
            @foreach($roleVal['child'] as $childKey=>$childVal)
        <ul class="hiddenRow collapse packageDetails" bgcolor="#C1C1C1">

            <li><input type="checkbox" id="submenuChk" value="<?php echo $id = $childVal['id']; ?>" name="admin_sub_menu_id[]" /></li>
            <li></li>
        </ul>
            @endforeach
        @endif
        </ul>
        <?php $count++; ?>
    @endforeach
@endif

Controller:-

$data = $request->all();
    $user_type_id = $data['user_type_id'];
    $admin_menu_id = $data['admin_menu_id'];
    $admin_sub_menu_id = $data['admin_sub_menu_id'];

    for ($i = 0; $i < count($data['admin_sub_menu_id']); $i++)
    {
        $role = new RoleMenu;
        $role->user_type_id = $user_type_id;
        $role->admin_menu_id = $admin_menu_id[$i];
        $role->admin_sub_menu_id = $admin_sub_menu_id[$i];
        $role->save();
    }

It inserts data when i check one admin_menu_id checkbox and one admin_sub_menu_id checkbox but when try to check more than one it throws an error of Undefined Offset. I am completely new to laravel and these array games!! Kindly help me out on this.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire