2016-11-24 7 views
0

私の仕事管理者では、ユーザーに役割を割り当てます。割り当てはチェックボックスを使用して行われます。ただし、既に割り当てられているユーザーを受け入れる場合、チェックボックスはチェックされません。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\[email protected]'); 
    } 
} 

//ビュー

@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 
+0

既に割り当てられているロールのリストを渡してから、チェックボックスをオンにする必要があります。私はあなたが現時点でそれをやっているのを見ません。 –

答えて

0

使用したクエリは、前の画面で選択した従業員のために既に割り当てられた役割をフェッチします。それをビューに渡します。そして、役割を割り当てる際に、役割IDが一致した場合にチェックします。

関連する問題