如何在laravel中选中一个复选框?

时间:2016-11-24 09:42:11

标签: laravel-5.2

在我的laravel工作中,管理员将角色分配给用户。使用复选框进行分配。但是在获取已分配的用户时,不勾选复选框。请选中已分配的复选框。

//控制器

<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use Session;
use Redirect;
use Response;
use App\Http\Controllers\AdminController;
use App\User;
use App\Area;
use App\Http\Requests\ShopUpdateRequest;
use DB;
use App\Customer;
use App\MyRoles;
use App\Roles;
use Assets;
use Auth;
use Datatables;

class EmployeeController extends AdminController
{

    public function assignrole(){
        $title = "Prizes";  
        $post = 0;
        $count = 0;
        $employees = User::where('employee','yes')->get();
        $roles = Roles::select('roles.role_name','roles.id')->get();

        return view('app.admin.roles.assignrole',  compact('title','post','count','roles','employees'));
    }


    public function postassignrole(Request $request){
        $title = "Prizes";  
        $post = 0;
        $count = 0;
        $employee = User::where('id',$request->employee)->pluck('username');//current employee name
        $emp_id = $request->employee;//current employee id
        $roles = Roles::select('roles.role_name','roles.id')->get();//all links
        return view('app.admin.roles.postassignrole',compact('title','post','count','roles','employee','emp_id'));
    }

    public function saverole(Request $request){
        $array = $request->all();//employee id
        $emp_id = $array['emp_id'];//after assigning roles gives employee id
        $i = 0;
        MyRoles::where('user_id',$emp_id)->delete();
        foreach($array as $data){

            if($i == 0){

            }else{
                MyRoles::create(
                    [
                        'user_id' => $emp_id,
                        'role_id' => $data
                    ]
                    );
            }
            $i++;

        }
        Session::flash('flash_notification', array('level' => 'success', 'message' => 'customer created successfully'));
        return Redirect::action('Admin\EmployeeController@assignrole');
    }
}

//视图

@extends('app.admin.layouts.default')

{{-- Web site Title --}}
@section('title') {{{ $title }}} :: @parent @stop


@section("styles") @parent
<style>
.material-switch > input[type="checkbox"] {
    display: none;   
}

.material-switch > label {
    cursor: pointer;
    height: 0px;
    position: relative; 
    width: 40px;  
}

.material-switch > label::before {
    background: rgb(0, 0, 0);
    box-shadow: inset 0px 0px 10px rgba(0, 0, 0, 0.5);
    border-radius: 8px;
    content: '';
    height: 16px;
    margin-top: -8px;
    position:absolute;
    opacity: 0.3;
    transition: all 0.4s ease-in-out;
    width: 40px;
}
.material-switch > label::after {
    background: rgb(255, 255, 255);
    border-radius: 16px;
    box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.3);
    content: '';
    height: 24px;
    left: -4px;
    margin-top: -8px;
    position: absolute;
    top: -4px;
    transition: all 0.3s ease-in-out;
    width: 24px;
}
.material-switch > input[type="checkbox"]:checked + label::before {
    background: inherit;
    opacity: 0.5;
}
.material-switch > input[type="checkbox"]:checked + label::after {
    background: inherit;
    left: 20px;
}

</style>
@stop

{{-- Content --}}
@section('main')


    <div class="page-header">
        <h3>
           Assign roles
        </h3>
    </div>
<div class="form-group">


<div class="container">
    <div class="row">
     <label>Assign Roles of <b>{{$employee}} </b></label>
    </div>
       <div class="row">
    <form action="{{url('admin/save-role')}}" method="post">
            <input type="hidden" name="emp_id" id="emp_id" value="{{$emp_id}}">
            <br/><br/><br/><br/>
        <div class="col-sm-12 col-md-10">

            <div class="panel panel-default">
                <div class="panel-heading">Select Roles</div>

                       <ul class="list-group">
                       @foreach($roles as $data)
                           <li class="list-group-item">

                           <label><input type="checkbox"  id="checkbox{{$data->id}}" name="{{$data->id}}" value="{{$data->id}}">{{$data->role_name}}</label>  

                           </li>                
                        @endforeach
                        </ul>

            </div>  
              <div class="form-group">
                                <div class="col-md-6 col-md-offset-2">
                                    <button type="submit" class="btn btn-primary">
                                        Assign Roles 
                                    </button>
                                </div>
                        </div>

        </div>
        </form>  
    </div>
</div>

</div>

@endsection

@section("scripts") @parent
<script src="/js/color.js"></script>
    @stop

1 个答案:

答案 0 :(得分:0)

使用查询来获取在上一个屏幕中选择的员工的已分配角色。然后将其传递给视图。在分配角色时,如果角色ID匹配,则将其标记为已选中。