显示属于用户类别的表



我的问题很简单,目前我的系统显示了一个表及其所有条目:

<table id="inventarios" class="table table-striped table-bordered shadow-lg mt-4" style="width: 100%">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Nombre</th>
<th scope="col">Unidad</th>
<th scope="col">Acciones</th>
</tr>
</thead>
<tbody>
@foreach($inventarios->where('uni_id',) as $inventario )
<tr>
<td>{{$inventario->id}}</td>
<td>{{$inventario->nombre}}</td>
<td>{{$inventario->uni_id}}</td>
<td>
<form action="{{ route ('inventarios.destroy',$inventario->id) }}" method="POST">
<a href="/inventarios/{{$inventario->id}}/edit" class="btn btn-info">Editar</a>
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger">Borrar</button>
</form>
</td>
</tr>
@endforeach

问题是,只有标记为ADMIN的用户才能查看所有条目,而其他用户只能查看属于其单元的表。

每个创建的用户都属于一个单元;Inventarios";。我想知道如何让用户看到他们自己的";Inventarios";而不是表上的所有条目。

我使用的是Laravel 8,PHP。

一旦定义了用户角色(Admin、Unit user等(,您就可以部署类似laravel权限的东西,这样可以节省大量代码,并在这方面为您提供帮助。另一种是检查已登录的用户,如Auth::user((->hasRole("管理"(

public function index()
{
$inventarios = '';
if(Auth::user()->hasRole('Admin'))
$inventarios = Inventario::all();
else
$inventario = Inventario::where('uni_id',Auth::user()->uni_id)->get();
return view( .... , compact('inventarios'));
}

我认为这样的东西可以工作

最新更新