【问题标题】:I Want to Disable Delete Button Base On auth()->user()->id (LARAVEL 8)我想禁用删除按钮基于 auth()->user()->id (LARAVEL 8)
【发布时间】: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


【解决方案1】:
    <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>

【讨论】:

  • 我已经解决了,我只是替换这个代码:@if (auth()-&gt;user()-&gt;id) &lt;button class="btn btn-outline-danger btn-xs" disabled&gt;&lt;i class="fe fe-trash"&gt;&lt;/i&gt;Delete&lt;/button&gt; @else &lt;button class="btn btn-outline-danger btn-xs"&gt;&lt;i class="fe fe-trash" &lt;/i&gt;Delete&lt;/button&gt; @endif 用这个代码:&lt;button class="btn btn-outline-dangerbtn-xs" @if($user-&gt;id === auth()-&gt;id()) disabled @endif&gt;&lt;i class="fe fe-trash"&gt;&lt;/i&gt; Delete&lt;/button&gt; 我从兄弟@Rwd 那里得到的。
  • 谢谢你的朋友。
【解决方案2】:

您需要检查$user-&gt;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>

【讨论】:

  • 天哪,它成功了。非常感谢你。
猜你喜欢
  • 1970-01-01
  • 2019-06-27
  • 1970-01-01
  • 2019-07-13
  • 2021-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多