【发布时间】:2022-01-07 23:44:04
【问题描述】:
我已经在该表上创建了用户索引,其中显示除密码之外的所有用户数据,并在最后一个字段中为删除用户设置了操作。问题是我只想为当时登录的用户禁用删除按钮。但我所做的是所有用户删除按钮都被禁用,请帮我解决这个问题。我试图找到tutoril,但还没有找到。在我的条件代码下方:
@if (auth()->user()->id)
<button class="btn btn-outline-danger
btn-xs" disabled><i class="fe fe-trash"></i>Delete</button>
@else
<button class="btn btn-outline-danger
btn-xs"><i class="fe fe-trash"></i>Delete</button>
@endif
在 UserController 中显示所有用户数据我使用此代码:
public function index(){
$users = User::all();
return view('admin.users.index', ['users'=> $users]);
}
在 index.blade 中我应用此代码来显示所有数据:
<table id="dataTableBasic" class="table" style="width:100%">
<thead>
<tr>
<th>{{ __('Username') }}</th>
<th>{{ __('Name') }}</th>
<th>{{ __('Email') }}</th>
<th>{{ __('Role') }}</th>
<th>{{ __('Posts') }}</th>
<th>{{ __('Action') }}</th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td><a href="{{route('user.profile.show', $user->id)}}" class="text-inherit link-primary">{{$user->username}}</a></td>
<td>{{$user->name}}</td>
<td><a href="mailto:{{$user->email}}" class="text-inherit link-primary">{{$user->email}}</a></td>
<td>Administrator</td>
<td><a href="#" class="text-inherit">100</a></td>
<td>
<form action="{{route('users.destroy',$user->id)}}" method="post" enctype="multipart/form-data">
@csrf
@method('DELETE')
@if (auth()->user()->id)
<button class="btn btn-outline-danger btn-xs" disabled><i class="fe fe-trash"></i>{{ __('Hapus') }}</button>
@else
<button class="btn btn-outline-danger btn-xs"><i class="fe fe-trash"></i>{{ __('Hapus') }}</button>
@endif
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
截图如下:
【问题讨论】:
-
您必须决定要禁用删除按钮的依据。就像当前登录的用户匹配进入循环的用户 id
-
解决了,伙计。谢谢你。
标签: php laravel conditional-statements