Filament Shield

In my app, all of users give me bool '1' mean {{ auth()->user()->isSuperAdmin() }}. Here is code. User.php
public function isSuperAdmin(): bool
{
return auth()->user()
->hasRole('super_admin');
}
public function isSuperAdmin(): bool
{
return auth()->user()
->hasRole('super_admin');
}
20 Replies
Tin
Tin2y ago
When you echo a bool it goes to 1 or 0 it wont echo to 'true'
Shaung Bhone
Shaung BhoneOP2y ago
1
toeknee
toeknee2y ago
you need to var_dump() to get the try var value and explanation. echoing a bool returns an integer
Shaung Bhone
Shaung BhoneOP2y ago
<div>
<div class="text-primary-red"
@auth
@class([
'text-primary-blue' => auth()->user()->isSuperAdmin(),
'text-primary-red' => !auth()->user()->isSuperAdmin(),
])
@endauth
>
dump -> {{ auth()->user()->isSuperAdmin() }}
{{ $feeling->user->name }}
</div>
<div>
<div class="text-primary-red"
@auth
@class([
'text-primary-blue' => auth()->user()->isSuperAdmin(),
'text-primary-red' => !auth()->user()->isSuperAdmin(),
])
@endauth
>
dump -> {{ auth()->user()->isSuperAdmin() }}
{{ $feeling->user->name }}
</div>
Shaung Bhone
Shaung BhoneOP2y ago
Shaung Bhone
Shaung BhoneOP2y ago
this user isn't superadmin
toeknee
toeknee2y ago
@dump(auth()->user()->isSuperAdmin()) What does it give?
Shaung Bhone
Shaung BhoneOP2y ago
it's true
Shaung Bhone
Shaung BhoneOP2y ago
toeknee
toeknee2y ago
So that code you are using is checking if the current user logged in is a superAdmin(). It's saying they are, if that's true then your issue is either 1. Your SuperAdmin function. Or two, you are expecting that code to check the record and not the logged in user currently viewing.
Shaung Bhone
Shaung BhoneOP2y ago
How can I solve that? 🥹
toeknee
toeknee2y ago
Should the function be checking the logged in user viewing the resource/
Shaung Bhone
Shaung BhoneOP2y ago
public function isSuperAdmin(): bool
{
if (auth()->check()) {
return auth()
->user()
->hasRole('super_admin');
}
}
public function isSuperAdmin(): bool
{
if (auth()->check()) {
return auth()
->user()
->hasRole('super_admin');
}
}
like that?
toeknee
toeknee2y ago
You still haven't answered my question
Tin
Tin2y ago
@shaungbhone what do you want to achieve exactly? Do you want to show something for example a badge if the user is a super-admin?
Shaung Bhone
Shaung BhoneOP2y ago
Yes sure yes
toeknee
toeknee2y ago
Just return: return auth() ->user() ->hasRole('super_admin'); Which should check the current user has the role super_admin assigned to them
Shaung Bhone
Shaung BhoneOP2y ago
I want to be sure my function isSuperAdmin() is correct or not!
toeknee
toeknee2y ago
So you need to check / test that then. Please this is basic php/laravel stuff with spatie permissions / roles. Not really filament related.
Shaung Bhone
Shaung BhoneOP2y ago
Ok thank you I'll check it out.
Want results from more Discord servers?
Add your server