我在該表上創建了用戶索引,顯示除密碼之外的所有用戶資料,并在最后一個欄位中為洗掉用戶設定了操作。問題是我只想為當時登錄的用戶禁用洗掉按鈕。但是我所做的是禁用所有用戶洗掉按鈕,請幫我解決這個問題。我試圖找到tutorial但還沒有找到。在我的條件代碼下方:
@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 hljs-string">" disabled><i hljs-string">"></i>{{ __('Hapus') }}</button>
@else
<button hljs-string">"><i hljs-string">"></i>{{ __('Hapus') }}</button>
@endif
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
截圖下方:

uj5u.com熱心網友回復:
您需要檢查是否$user->id與經過身份驗證的用戶的 ID 相同:
@if(auth()->id() === $user->id)
此外,由于disabled屬性中按鈕之間的唯一不同,您可以執行以下操作:
<button class="btn btn-outline-dangerbtn-xs"
@if($user->id === auth()->id()) disabled @endif>
<i class="fe fe-trash"></i>
Delete
</button>
uj5u.com熱心網友回復:
<td>
<form action="{{route('users.destroy',$user->id)}}" method="post" enctype="multipart/form-data">
@csrf
@method('DELETE')
@if (auth()->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
</form>
</td>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/370929.html
