laravel中的角色和责任

时间:2017-08-23 11:51:02

标签: php laravel

我正在创建代码必须遵循的角色和职责:

public function boot()
{
    view()->composer('admin.sidebar', function ($view) {
$moduleRs = DB::table('users')
    ->join('permissions','users.role', '=', 'permissions.role_id') 
    ->select('users.role as usersrole','permissions.role_id as role_id','permissions.module_name as module_name')
    ->where('users.role', '=', \Auth::user()->role)
    ->get();
   // dd($moduleRs[0]->module_name);
   $moduleData = null;

    if ( count($moduleRs) > 0 ) {
     $result = [];
      foreach($moduleRs as $row){
        if(!isset($result[$row->role_id])) {
            $result[$row->role_id] = array(
                'role_id' => $row->role_id,
                'module_name' => [$row->module_name],
        );
        }else{
            $result[$row->role_id]['module_name'] = array_merge($result[$row->role_id]['module_name'], [$row->module_name] );
           }
    }


    $data=$moduleRs[0]->module_name;
    dd($data);
    $splittedstring[]=explode(" ,",$data);
    //dd($splittedstring);
    foreach ($splittedstring as $key => $value) {
    echo "  '.$value.'<br>";
    }  
} 

$view->with('moduleData', $moduleData);
});

 }

在上面的代码中,当我可以使用dd($ data)时,它可以显示用户权限的id。但是当隐藏dd($ data)意味着它也无法生成结果。请提供一个想法。

enter image description here

当我可以隐藏dd(...)时,它会像上面的屏幕截图一样显示..

when i put dd($splittedstring) means it shows this found below

   array:3 [▼
   0 => "0"
   1 => "4"
   2 => "5"
   ]

这是在下面的屏幕截图中找到的模块我可以将该模块存储在模块表的模块字段名称中

[![在此处输入图像说明] [2]] [2]

这是下面找到的权限表单,它必须从模块表中获取模块名称并将此值存储到module_name的权限表字段名称,但它是一个整数而不是字符串这是一个外键。 [![在此处输入图像说明] [3]] [3]

并在视图中显示如下所示。这里是角色1 =&gt;超级管理员0 =&gt;管理员...我可以将权限管理员设为0 =&gt;仪表板,4 =&gt;员工,5 =&gt;领导......当我以管理员身份登录时,我只能查看仪表板,员工和潜在客户

权限的create.blade.php

        <div class="col-md-9">
            <div class="panel panel-default">
                <div class="panel-heading">Create New Permission</div>
                <div class="panel-body">
                    <a href="{{ url('/permission/permission') }}" title="Back"><button class="btn btn-warning btn-xs"><i class="fa fa-arrow-left" aria-hidden="true"></i> Back</button></a>
                    <br />
                    <br />

                    @if ($errors->any())
                        <ul class="alert alert-danger">
                            @foreach ($errors->all() as $error)
                                <li>{{ $error }}</li>
                            @endforeach
                        </ul>
                    @endif

                    {!! Form::open(['url' => '/permission/permission', 'class' => 'form-horizontal', 'files' => true]) !!}

                    @include ('permission.permission.form')

                    {!! Form::close() !!}

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

Permission's form.blade.php

<div class="form-group {{ $errors->has('role_id') ? 'has-error' : ''}}">
{!! Form::label('role_id', 'Role', ['class' => 'col-md-4 control-label']) 
!!}
<div class="col-md-6">  

    {!! Form::select('role_id',($role),null,['class' => 'form-control']) !!}
     {!! $errors->first('role_id', '<p class="help-block">:message</p>') !!}
   </div>
</div><div class="form-group {{ $errors->has('module_name') ? 'has-error':     ''}}">
   {!! Form::label('module_name', 'Module', ['class' => 'col-md-4 control-
   label']) !!}
   <div class="col-md-6"> 
   {!! Form::select('module_name[]',($module), null, ['class' => 'form-
  control','multiple' => true]) !!} 
  {!! $errors->first('module_name', '<p class="help-block">:message</p>') 
   !!}


   </div>

  </div> 

   <div class="form-group">
   <div class="col-md-offset-4 col-md-4">
      {!! Form::submit(isset($submitButtonText) ? $submitButtonText : 
      'Create', ['class' => 'btn btn-primary']) !!} 
   </div>
</div>

permissionController.php存储并创建函数

  public function create()
{
    $role = \DB::table('roles')->pluck('name');

    $module = \DB::table('modules')->pluck('module'); 
   return view('permission.permission.create',array('role'=>$role,'module'=>$module)); 

}

/**
 * Store a newly created resource in storage.
 *
 * @param \Illuminate\Http\Request $request
 *
 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
 */
public function store(Request $request)
{

    $requestData = $request->all();
    $news = $request->input('module_name');
    $news = implode(',', $news);
    $input = $request->except('module_name');
   //Assign the "mutated" news value to $input
        $input['module_name'] = $news;
    Permission::create($input);

    Session::flash('flash_message', 'Permission added!');

    return redirect('permission/permission');

}

 public function show($id)
{
    $permission = Permission::findOrFail($id);

    return view('permission.permission.show', compact('permission'));
}

show.blade.php

  <div class="col-md-9">
            <div class="panel panel-default">
                <div class="panel-heading">Permission {{ $permission->id }}</div>
                <div class="panel-body">

                    <a href="{{ url('/permission/permission') }}" title="Back"><button class="btn btn-warning btn-xs"><i class="fa fa-arrow-left" aria-hidden="true"></i> Back</button></a>
                    <a href="{{ url('/permission/permission/' . $permission->id . '/edit') }}" title="Edit Permission"><button class="btn btn-primary btn-xs"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Edit</button></a>
                    {!! Form::open([
                        'method'=>'DELETE',
                        'url' => ['permission/permission', $permission->id],
                        'style' => 'display:inline'
                    ]) !!}
                        {!! Form::button('<i class="fa fa-trash-o" aria-hidden="true"></i> Delete', array(
                                'type' => 'submit',
                                'class' => 'btn btn-danger btn-xs',
                                'title' => 'Delete Permission',
                                'onclick'=>'return confirm("Confirm delete?")'
                        ))!!}
                    {!! Form::close() !!}
                    <br/>
                    <br/>

                    <div class="table-responsive">
                        <table class="table table-borderless">
                            <tbody>
                                <tr>
                                    <th>ID</th><td>{{ $permission->id }}</td>
                                </tr>
                                <tr><th> Role </th><td> {{ $permission->role_id }} </td></tr><tr><th> Module </th><td> {{ $permission->module_name }} </td></tr>
                            </tbody> 

                        </table>
                    </div>

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

enter image description here

1 个答案:

答案 0 :(得分:0)

您应该更改create中的permissioncontroller功能,如:

permissionController.php

public function create()
{
  $role = \DB::table('roles')->pluck('name');

  $module = \DB::table('modules')->get();

  $arrModule = array();

  foreach($module as $modules){

    $arrModule[$modules->id] = $modules->module;

  }

  return view('permission.permission.create',array('role'=>$role,'module'=>$arrModule)); 

}

更新了答案

public function show($id)
{
    $permission = Permission::findOrFail($id);

    $permissionModule =explode(",",$permission->module);



    $module = \DB::table('modules')->get();

    $arrPermissionModule = array();

    foreach($module as $modules){

      if(in_array($modules->id,$permissionModule)){

        $arrPermissionModule[$modules->id] = $modules->module;

      }

    }

    return view('permission.permission.show', compact('permission','arrPermissionModule'));
}

<强> Show.blade.php

<div class="col-md-9">
            <div class="panel panel-default">
                <div class="panel-heading">Permission {{ $permission->id }}</div>
                <div class="panel-body">

                    <a href="{{ url('/permission/permission') }}" title="Back"><button class="btn btn-warning btn-xs"><i class="fa fa-arrow-left" aria-hidden="true"></i> Back</button></a>
                    <a href="{{ url('/permission/permission/' . $permission->id . '/edit') }}" title="Edit Permission"><button class="btn btn-primary btn-xs"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Edit</button></a>
                    {!! Form::open([
                        'method'=>'DELETE',
                        'url' => ['permission/permission', $permission->id],
                        'style' => 'display:inline'
                    ]) !!}
                        {!! Form::button('<i class="fa fa-trash-o" aria-hidden="true"></i> Delete', array(
                                'type' => 'submit',
                                'class' => 'btn btn-danger btn-xs',
                                'title' => 'Delete Permission',
                                'onclick'=>'return confirm("Confirm delete?")'
                        ))!!}
                    {!! Form::close() !!}
                    <br/>
                    <br/>

                    <div class="table-responsive">
                        <table class="table table-borderless">
                            <tbody>
                                <tr>
                                    <th>ID</th><td>{{ $permission->id }}</td>
                                </tr>
                                <tr><th> Role </th><td> {{ $permission->role_id }} </td></tr>
                                <tr><th> Module </th>
                                  <td> 
                                    @foreach($arrPermissionModule as $arrPermissionModules)
                                      {{ $arrPermissionModules }} 
                                    @endforeach
                                  </td>
                                </tr>
                            </tbody> 

                        </table>
                    </div>

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