AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

User.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Models;
  3. use Carbon\CarbonInterface;
  4. use Database\Factories\UserFactory;
  5. use Illuminate\Contracts\Auth\MustVerifyEmail;
  6. use Illuminate\Database\Eloquent\Attributes\Fillable;
  7. use Illuminate\Database\Eloquent\Attributes\Hidden;
  8. use Illuminate\Database\Eloquent\Factories\HasFactory;
  9. use Illuminate\Database\Eloquent\Relations\HasMany;
  10. use Illuminate\Database\Eloquent\Relations\HasOne;
  11. use Illuminate\Foundation\Auth\User as Authenticatable;
  12. use Illuminate\Notifications\Notifiable;
  13. use Illuminate\Support\Carbon;
  14. use Laravel\Fortify\TwoFactorAuthenticatable;
  15. /**
  16. * @property int $id
  17. * @property string $name
  18. * @property string $email
  19. * @property CarbonInterface|null $trial_ends_at
  20. * @property-read Subscription|null $subscription
  21. * @property Carbon|null $email_verified_at
  22. * @property string $password
  23. * @property string|null $two_factor_secret
  24. * @property string|null $two_factor_recovery_codes
  25. * @property Carbon|null $two_factor_confirmed_at
  26. * @property string|null $remember_token
  27. * @property Carbon|null $created_at
  28. * @property Carbon|null $updated_at
  29. */
  30. #[Fillable(['name', 'email', 'password'])]
  31. #[Hidden(['password', 'two_factor_secret', 'two_factor_recovery_codes', 'remember_token', 'subscription'])]
  32. class User extends Authenticatable implements MustVerifyEmail
  33. {
  34. /** @use HasFactory<UserFactory> */
  35. use HasFactory, Notifiable, TwoFactorAuthenticatable;
  36. /**
  37. * Get the attributes that should be cast.
  38. *
  39. * @return array<string, string>
  40. */
  41. protected function casts(): array
  42. {
  43. return [
  44. 'email_verified_at' => 'datetime',
  45. 'password' => 'hashed',
  46. 'two_factor_confirmed_at' => 'datetime',
  47. 'trial_ends_at' => 'datetime',
  48. ];
  49. }
  50. protected static function booted(): void
  51. {
  52. // Every new account starts with a free trial.
  53. static::creating(function (User $user): void {
  54. $user->trial_ends_at ??= now()->addDays((int) config('plans.trial_days', 14));
  55. });
  56. }
  57. /**
  58. * The paid plan, if this user ever started one.
  59. *
  60. * @return HasOne<Subscription, $this>
  61. */
  62. public function subscription(): HasOne
  63. {
  64. return $this->hasOne(Subscription::class);
  65. }
  66. public function onTrial(): bool
  67. {
  68. return $this->trial_ends_at !== null && $this->trial_ends_at->isFuture();
  69. }
  70. /**
  71. * The plan this user is on right now: the paid plan, or the free trial while it lasts, or nothing.
  72. */
  73. public function currentPlan(): ?Plan
  74. {
  75. $subscription = $this->subscription;
  76. if ($subscription !== null && $subscription->isActive()) {
  77. return $subscription->plan;
  78. }
  79. return $this->onTrial() ? Plan::findByKey((string) config('plans.trial_plan')) : null;
  80. }
  81. /**
  82. * The stores this user owns.
  83. *
  84. * @return HasMany<Store, $this>
  85. */
  86. public function stores(): HasMany
  87. {
  88. return $this->hasMany(Store::class);
  89. }
  90. }