Tuesday, March 21, 2017

how to check for 'checked' checkbox - php

I am trying to send checked value from view to database in checkboxes but system either takes all of them checked or none. I am using Laravel 5.3 with php 5. Here is my checkbox view

<?php
            foreach($tests as $test) {
                if($test == 1) {
                    echo "<input type='checkbox' name='check' value='1' checked/><br>";
                }               
                else {
                    echo "<input type='checkbox' name='check'/><br>";
                }

            }
        ?>

And my function to check and attach permission to role is

public function updatepermission($id)
{
    $role = Role::find($id);
    $permissions = Permission::all();
    $role->permissions()->detach();
    foreach ($permissions as $key) {
        if (Input::get('check') === '1') {
            $role->permissions()->attach($key);
        }
    }
    return redirect('role')->with('message','Permissions has been updated');
}

Any help will be highly appreciated.



via MAYANK BHATI

Advertisement