From 5b62f359e3d3dcd09752d9f15cf0c9d01a378ebe Mon Sep 17 00:00:00 2001 From: Refringe Date: Sun, 23 Jun 2024 08:37:16 -0400 Subject: [PATCH] Null Guard Admin Check Also adds method to check if a user is a moderator. --- app/Models/User.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/Models/User.php b/app/Models/User.php index a5ef6ef..3429578 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -72,9 +72,14 @@ class User extends Authenticatable implements MustVerifyEmail return $this->belongsTo(UserRole::class, 'user_role_id'); } + public function isMod(): bool + { + return Str::lower($this->role?->name) === 'moderator' || $this->isAdmin(); + } + public function isAdmin(): bool { - return Str::lower($this->role->name) === 'administrator'; + return Str::lower($this->role?->name) === 'administrator'; } protected function casts(): array