@if(auth()->user()->isAdmin())content@endif@if(auth()->user()->isNormal())content @endif然后如果用戶未登錄,我會收到此錯誤:調用(user.blade.php 的路徑)isAdmin()上的成員函數null
3 回答

慕標琳琳
TA貢獻1830條經驗 獲得超9個贊
這個錯誤,Call to a member function isAdmin() on null告訴你的是,沒有任何對象附加到你調用函數的任何地方。在這種情況下,它是經過身份驗證的用戶。這意味著當您調用isAdmin().
您可以通過首先檢查用戶是否登錄來避免它。@Alberto 為您提供了不同的選擇。此外,您可以if像這樣調整您的陳述:
@if( auth()->check() && auth()->user()->isAdmin() )
...
@if( auth()->check() && auth()->user()->isNormal() )
在這里,第一個條件檢查是否有經過身份驗證的用戶,第二個條件只有在第一個語句返回時才會檢查true。
附帶說明一下,您可以使用Policiesor/andGate來實現更強大的授權邏輯。

慕雪6442864
TA貢獻1812條經驗 獲得超5個贊
You can change your code like shown below
@auth
content if user is logged in
@endauth
@guest
content if user is not logged in
@endguest
- 3 回答
- 0 關注
- 187 瀏覽
添加回答
舉報
0/150
提交
取消