我想禁用基于 auth()->user()->id (LARAVEL 8) 的删除按钮

I Want to Disable Delete Button Base On auth()->user()->id (LARAVEL 8)

我已经创建了用户索引,其中 table 显示了除密码之外的所有用户数据,并且在最后一个字段中我为删除用户设置了操作。问题是我只想为当时登录的用户禁用删除按钮。但是我所做的是禁用所有用户删除按钮,请帮我解决这个问题。我试图找到 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>

截图下方:

您需要检查$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>
    <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>