如何以表格格式显示结果

时间:2020-10-20 09:58:19

标签: laravel

我想以表格格式显示权限表的结果...我正在使用Laratrust来获取角色和权限

我的RoleController

 public function show($id)
    {
        $role = Role::with('permissions')->with('users')->where('id', $id)->first();
        return view('admin.roles.show', compact('role'));  
    }

我希望结果像我对其进行硬编码的方式一样在此表中水平显示

show.blade.php

<table class="table">
    <thead class="thead-dark">
        <tr>
            <th scope="col">PERMISSION</th>
            <th scope="col">CREATE</th>
            <th scope="col">READ</th>
            <th scope="col">UPDATE</th>
            <th scope="col">DELETE</th>
        </tr>
    </thead>
    <tbody>
    <tr>
        <th scope="col">Users</th>
        <td>Create User</td>
        <td>Read User</td>
        <td>Update User</td>
        <td>Delete User</td>
    </tr>
    <tr>
        <th scope="col">Profile</th>
        <td>Create Profile</td>
        <td>Read Profile</td>
        <td>Update Profile</td>
        <td>Delete Profile</td>
    </tr>
    </tbody>
</table>

2 个答案:

答案 0 :(得分:0)

<thead class="thead-dark">
  <tr>
    <th scope="col">PERMISSION</th>
    <th scope="col">CREATE</th>
    <th scope="col">READ</th>
    <th scope="col">UPDATE</th>
    <th scope="col">DELETE</th>
  </tr>
</thead>
<tbody>
   @foreach($role as $key => $data)
     <tr>    
       <th>{{$data->permission}}</th>
       <th>{{$data->create}}</th>
       <th>{{$data->read}}</th>
       <th>{{$data->update}}</th>
       <th>{{$data->delete}}</th>                 
    </tr>
  @endforeach
</tbody>

如果角色变量具有所有数据,我认为这应该起作用

答案 1 :(得分:0)

您可以在Laravel文档中阅读更多信息https://laravel.com/docs/8.x/eloquent-relationships#eager-loading

并确保您的角色模型中具有权限,用户关系,然后尝试以下操作:


// controller file
public function show($id)
{
        $role = Role::with(['permissions', 'users'])->where('id', $id)->first();
        return view('admin.roles.show', compact('role'));  
}

// blade file
<table class="table">
    <thead class="thead-dark">
        <tr>
            <th scope="col">PERMISSION</th>
            <th scope="col">CREATE</th>
            <th scope="col">READ</th>
            <th scope="col">UPDATE</th>
            <th scope="col">DELETE</th>
        </tr>
    </thead>
    <tbody>
@foreach($role->permissions as $permission)
    <tr>
        <th scope="col">{{ $permission->name }}</th>
        <td>Create User</td>
        <td>Read User</td>
        <td>Update User</td>
        <td>Delete User</td>
    </tr>
@endforeach
    </tbody>
</table>