I have two different tables where two different kind of users are stored.
The first are Portalusers, the second are employees.
Now I have an overview page of all posts, where Portalusers should be able to use a filter button for a department.
Employees should be able to use it aswell, IF their role IS NOT "FBL".
So I tried it with this code:
@if(auth()->user()->Rolle != 'FBL')
<div >
<select name="abteilung" id="abteilung" >
<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
But when no-one is authenticated, so a guest is using the page I get an error Attempt to read property "Rolle" on null
- which makes sense since no-one is authenticated.
But I also tried putting this before the @if
:
@auth('web')
@auth('portal')
@auth('guest')
I have two guards, the one is the default laravel guard and the second one is my custom portal
guard for portal users.
Is there any way I can make this work, so that the button is available when guests, portal users and employees which don't have the 'Rolle = FBL`, but disappears when someone with this specific role is logged in?
CodePudding user response:
You need to check if user is logged in before trying to access the user object.
To have it visible for guests and users with Rolle
not as FBL
, use this:
@if( ! auth()->check() || auth()->user()->Rolle != 'FBL')
https://laravel.com/docs/8.x/authentication#determining-if-the-current-user-is-authenticated
CodePudding user response:
I think, in your case, the best solution is:
1: Check if the user is logged in.
2: Check the role but instead of writing too much "ifs" I would do it like this (following your code)
//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
});
}
// In your blade file you can use the "@notfbl($user)" in every part you need and also create more
@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 >
<select name="abteilung" id="abteilung" >
<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
CodePudding user response:
You need to use nested if statement i.e
// check if user is a guests, portal users or employees
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
}
}