我有兩個不同的表,其中存盤了兩種不同型別的用戶。
第一個是門戶用戶,第二個是員工。
現在我有一個所有帖子的概覽頁面,Portalusers 應該能夠在其中使用部門的過濾器按鈕。
員工也應該能夠使用它,如果他們的角色不是“FBL”。
所以我用這個代碼試了一下:
@if(auth()->user()->Rolle != 'FBL')
<div class="text-left mb-4 h-12">
<select name="abteilung" id="abteilung" class="h-full w-full flex justify-center bg-white bg-opacity-95 rounded focus:ring-2 border border-gray-300 focus:border-indigo-500 text-base
outline-none text-gray-700 text-lg text-center leading-8">
<option selected="true" disabled="false">Abteilung</option>
@foreach($abteilungs as $abteilung)
<option value="{{ $abteilung->abteilung_name }}">{{ $abteilung->abteilung_name }}</option>
@endforeach
<option value="alle">Alle Abteilungen</option>
</select>
</div>
@endif
但是當沒有人通過身份驗證時,因此訪客正在使用該頁面,我收到一個錯誤Attempt to read property "Rolle" on null- 這是有道理的,因為沒有人通過身份驗證。
但我也試過把它放在@if:
@auth('web')
@auth('portal')
@auth('guest')
我有兩個守衛,一個是默認的 Laravel 守衛,第二個是我portal為門戶用戶定制的守衛。
有什么方法可以讓我完成這項作業,以便在沒有“Rolle = FBL”的客人、門戶用戶和員工時可以使用該按鈕,但當具有此特定角色的人登錄時該按鈕會消失?
uj5u.com熱心網友回復:
在嘗試訪問用戶物件之前,您需要檢查用戶是否已登錄。
要讓來賓和用戶看到它Rolle不是 as FBL,請使用:
@if( ! auth()->check() || auth()->user()->Rolle != 'FBL')
https://laravel.com/docs/8.x/authentication#determining-if-the-current-user-is-authenticated
uj5u.com熱心網友回復:
我認為,就您而言,最好的解決方案是:
1:檢查用戶是否登錄。
2:檢查角色,但不要寫太多“如果”,我會這樣做(按照您的代碼)
//In your folder providers->AppServiceProvider.php -> method "boot"
public function boot()
{
Blade::if('notfbl', function (User $user) { // you name it as you want I named it "notfbl", the parameter is the logged user
return $user->id == auth()->user()->role_id != 1; // Here I assumed that the id of the FBL role is 1, you adjust it
});
}
// 在您的刀片檔案中,您可以在您需要的每個部分中使用“@notfbl($user)”,還可以創建更多
@auth // Everything inside "@auth" will be shown ONLY if the user is authenticated. If you want to do something for unauthenticated users you should write your html/code inside the "@guest @endguest" tags
@notfbl($user) // It will be shown ONLY for users with role different to FBL
<div class="text-left mb-4 h-12">
<select name="abteilung" id="abteilung" class="h-full w-full
flex justify-center bg-white bg-opacity-95 rounded focus:ring-2 border border-gray-300 focus:border-indigo-500 text-base outline-none text-gray-700 text-lg text-center leading-8">
<option selected="true" disabled="false">Abteilung</option>
@foreach($abteilungs as $abteilung)
<option value="{{ $abteilung->abteilung_name }}">{{ $abteilung->abteilung_name }}</option>
@endforeach
<option value="alle">Alle Abteilungen</option>
</select>
</div>
@endnotfbl
@endauth
uj5u.com熱心網友回復:
您需要使用嵌套的 if 陳述句,即
// 檢查用戶是訪客、門戶用戶還是員工
if user == guest
{
// write the code here for what guest should see
}
// so user is not guest
else
{
// check if user is employee or portal user
if user == employee
{
// write the code here for what employee should see
}
// user is not employee so user is portal user
else
{
// write the code here for what portal use should see
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/389878.html
標籤:php 拉拉维尔 laravel-blade 角色
