| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
-
- namespace App\Models;
-
- use Carbon\CarbonInterface;
- use Database\Factories\UserFactory;
- use Illuminate\Contracts\Auth\MustVerifyEmail;
- use Illuminate\Database\Eloquent\Attributes\Fillable;
- use Illuminate\Database\Eloquent\Attributes\Hidden;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use Illuminate\Database\Eloquent\Relations\HasOne;
- use Illuminate\Foundation\Auth\User as Authenticatable;
- use Illuminate\Notifications\Notifiable;
- use Illuminate\Support\Carbon;
- use Laravel\Fortify\TwoFactorAuthenticatable;
-
- /**
- * @property int $id
- * @property string $name
- * @property string $email
- * @property CarbonInterface|null $trial_ends_at
- * @property-read Subscription|null $subscription
- * @property Carbon|null $email_verified_at
- * @property string $password
- * @property string|null $two_factor_secret
- * @property string|null $two_factor_recovery_codes
- * @property Carbon|null $two_factor_confirmed_at
- * @property string|null $remember_token
- * @property Carbon|null $created_at
- * @property Carbon|null $updated_at
- */
- #[Fillable(['name', 'email', 'password'])]
- #[Hidden(['password', 'two_factor_secret', 'two_factor_recovery_codes', 'remember_token', 'subscription'])]
- class User extends Authenticatable implements MustVerifyEmail
- {
- /** @use HasFactory<UserFactory> */
- use HasFactory, Notifiable, TwoFactorAuthenticatable;
-
- /**
- * Get the attributes that should be cast.
- *
- * @return array<string, string>
- */
- protected function casts(): array
- {
- return [
- 'email_verified_at' => 'datetime',
- 'password' => 'hashed',
- 'two_factor_confirmed_at' => 'datetime',
- 'trial_ends_at' => 'datetime',
- ];
- }
-
- protected static function booted(): void
- {
- // Every new account starts with a free trial.
- static::creating(function (User $user): void {
- $user->trial_ends_at ??= now()->addDays((int) config('plans.trial_days', 14));
- });
- }
-
- /**
- * The paid plan, if this user ever started one.
- *
- * @return HasOne<Subscription, $this>
- */
- public function subscription(): HasOne
- {
- return $this->hasOne(Subscription::class);
- }
-
- public function onTrial(): bool
- {
- return $this->trial_ends_at !== null && $this->trial_ends_at->isFuture();
- }
-
- /**
- * The plan this user is on right now: the paid plan, or the free trial while it lasts, or nothing.
- */
- public function currentPlan(): ?Plan
- {
- $subscription = $this->subscription;
-
- if ($subscription !== null && $subscription->isActive()) {
- return $subscription->plan;
- }
-
- return $this->onTrial() ? Plan::findByKey((string) config('plans.trial_plan')) : null;
- }
-
- /**
- * The stores this user owns.
- *
- * @return HasMany<Store, $this>
- */
- public function stores(): HasMany
- {
- return $this->hasMany(Store::class);
- }
- }
|