*/ protected $fillable = [ 'name', 'email', 'notification_email', 'phone', 'password', 'role', 'color', 'token_fcm', ]; /** * The attributes that should be hidden for serialization. * * @var list */ protected $hidden = [ 'password', 'remember_token', ]; /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; } /** * Route notifications for the FCM channel. * * @return string */ public function routeNotificationForFcm(): string { return (string)$this->token_fcm; } public function getAppInstalledAttribute(): string { return $this->token_fcm ? 'Да' : 'Нет'; } public function userNotifications(): HasMany { return $this->hasMany(UserNotification::class); } public function unreadUserNotifications(): HasMany { return $this->userNotifications()->whereNull('read_at'); } public static function assignUniqueFcmToken(int $userId, string $token): void { DB::transaction(function () use ($userId, $token) { self::query() ->where('id', '!=', $userId) ->where('token_fcm', $token) ->update(['token_fcm' => null]); self::query() ->where('id', $userId) ->update(['token_fcm' => $token]); }); } public static function clearFcmToken(int $userId): void { self::query() ->where('id', $userId) ->update(['token_fcm' => null]); } }