| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- namespace App\Models;
- use Illuminate\Contracts\Auth\MustVerifyEmail;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Illuminate\Foundation\Auth\User as Authenticatable;
- use Illuminate\Notifications\Notifiable;
- use Illuminate\Support\Facades\DB;
- class User extends Authenticatable implements MustVerifyEmail
- {
- use HasFactory, Notifiable, SoftDeletes;
- const DEFAULT_SORT_BY = 'created_at';
- /**
- * The attributes that are mass assignable.
- *
- * @var list<string>
- */
- protected $fillable = [
- 'name',
- 'email',
- 'notification_email',
- 'phone',
- 'password',
- 'role',
- 'color',
- 'token_fcm',
- ];
- /**
- * The attributes that should be hidden for serialization.
- *
- * @var list<string>
- */
- protected $hidden = [
- 'password',
- 'remember_token',
- ];
- /**
- * Get the attributes that should be cast.
- *
- * @return array<string, string>
- */
- 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]);
- }
- }
|