W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
當編寫 Blade
模板時,可能希望僅在用戶被授權執(zhí)行給定操作時才顯示頁面的一部分。比如,你可能希望僅在用戶可以實際更新帖子時顯示博客帖子的更新表單。在這樣情況下,你可以使用 @can
和 @cannot
等一系列指令:
@can('update', $post)
<!-- 當前用戶可以更新文章 -->
@elsecan('create', App\Models\Post::class)
<!-- 當前用戶可以創(chuàng)建新文章 -->
@endcan
@cannot('update', $post)
<!-- 當前用戶不可以更新文章-->
@elsecannot('create', App\Models\Post::class)
<!--當前用戶不可以創(chuàng)建新文章-->
@endcannot
這些指令是編寫 @if
和 @unless
語句的快捷方法。 @can
和 @cannot
語句分別轉化為以下語句:
@if (Auth::user()->can('update', $post))
<!-- 當前用戶可以更新文章 -->
@endif
@unless (Auth::user()->can('update', $post))
<!-- 當前用戶不可以更新文章 -->
@endunless
您還可以確定用戶是否具有來自給定能力列表的任何授權能力。 要實現這一點,請使用 @canany
指令:
@canany(['update', 'view', 'delete'], $post)
// 當前用戶可以更新、查看、刪除文章
@elsecanany(['create'], \App\Models\::class)
// 當前用戶可以創(chuàng)建新文章
@endcanany
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯系方式:
更多建議: