*/ protected $fillable = [ 'name', 'email', 'role', 'dealer_id', 'password', 'role_id' ]; /** * The attributes that should be hidden for serialization. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ]; /** * Get all of the transactions for the User * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function transactions() { return $this->hasMany(Transaction::class, 'user_id', 'id'); } /** * Get all of the sa_transactions for the User * * @return \Illuminate\Database\Eloquent\Relations\HasMany */ public function sa_transactions() { return $this->hasMany(Transaction::class, 'user_sa_id', 'id'); } /** * Get the dealer associated with the User * * @return \Illuminate\Database\Eloquent\Relations\HasOne */ public function dealer() { return $this->hasOne(Dealer::class, 'id', 'dealer_id'); } /** * Get the role associated with the User * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function role() { return $this->belongsTo(Role::class, 'role_id'); } /** * Check if user has a specific role * * @param string $roleName * @return bool */ public function hasRole($roleName) { // If role_id is 0 or null, user has no role if (!$this->role_id) { return false; } // For admin role, we can check if user has admin privileges if (strtolower($roleName) === 'admin') { return $this->isAdmin(); } // Load role if not already loaded if (!$this->relationLoaded('role')) { $this->load('role'); } return $this->role && strtolower($this->role->name) === strtolower($roleName); } /** * Check if user is admin by checking admin privileges * * @return bool */ public function isAdmin() { // Check if user has admin privileges by checking if they can access admin area try { $adminPrivilege = \App\Models\Privilege::join('menus', 'menus.id', '=', 'privileges.menu_id') ->where('menus.link', 'adminarea') ->where('privileges.role_id', $this->role_id) ->where('privileges.view', 1) ->first(); return $adminPrivilege !== null; } catch (\Exception $e) { return false; } } }