$notifications * @property-read int|null $notifications_count * @property-read \Kalnoy\Nestedset\Collection $permissions * @property-read int|null $permissions_count * @property-read \Illuminate\Database\Eloquent\Collection $tokens * @property-read int|null $tokens_count * @method static \Database\Factories\UserFactory factory($count = null, $state = []) * @method static \Illuminate\Database\Eloquent\Builder|User newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|User newQuery() * @method static \Illuminate\Database\Eloquent\Builder|User query() * @method static \Illuminate\Database\Eloquent\Builder|User whereCreatedAt($value) * @method static \Illuminate\Database\Eloquent\Builder|User whereEmail($value) * @method static \Illuminate\Database\Eloquent\Builder|User whereEmailVerifiedAt($value) * @method static \Illuminate\Database\Eloquent\Builder|User whereId($value) * @method static \Illuminate\Database\Eloquent\Builder|User whereLanguage($value) * @method static \Illuminate\Database\Eloquent\Builder|User whereName($value) * @method static \Illuminate\Database\Eloquent\Builder|User wherePassword($value) * @method static \Illuminate\Database\Eloquent\Builder|User whereType($value) * @method static \Illuminate\Database\Eloquent\Builder|User whereUpdatedAt($value) * @mixin \Eloquent */ class User extends Authenticatable { use HasFactory, Notifiable, HasApiTokens; protected $guarded = ["id"]; /** * The attributes that should be hidden for serialization. * * @var array */ 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", "type" => UserTypeEnum::class, "language" => LanguageEnum::class, ]; } public function isAdmin(): bool { return $this->type === UserTypeEnum::ADMIN; } /** * Create a new access token for the user. */ public function createAccessToken(string $deviceId): string { return $this->createToken( name: "access_token_{$deviceId}", abilities: ["access"], expiresAt: Carbon::now()->addMinutes(15), )->plainTextToken; } /** * Create a new refresh token for the user. */ public function createRefreshToken(string $deviceId): string { return $this->createToken( name: "refresh_token_{$deviceId}", abilities: ["refresh"], expiresAt: Carbon::now()->addDays(30), )->plainTextToken; } /** * Delete all tokens (access and refresh) for a specific device. */ public function deleteTokensByDevice(string $deviceId): void { $this->tokens() ->where("name", "like", "%_{$deviceId}") ->delete(); } /** * @return BelongsToMany */ public function permissions(): BelongsToMany { return $this->belongsToMany( Permission::class, "user_type_permissions", "user_type", "permission_id", ); } }