Files
approagency/app/Models/User.php
T
2026-06-10 08:30:03 +00:00

107 lines
2.8 KiB
PHP
Executable File

<?php
namespace App\Models;
use App\Traits\HasUuid;
use App\Traits\HasReferralCode;
use DateTimeInterface;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Support\Str;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, HasUuid, HasReferralCode, Filterable;
const GENDERS = [
'male' => 1,
'female' => 2,
];
// Referral program (کد معرف): successful invites needed for a free month,
// and the app whose monthly product is granted as the reward.
const REFERRAL_SUBSCRIPTION_TARGET = 10;
const REFERRAL_PACKAGE_NAME = 'com.approagency.meditation';
protected $guarded = [];
protected $casts = [
'mobile_verified_at' => 'datetime',
'referral_subscription_granted_at' => 'datetime',
];
protected $hidden = [
'password',
];
protected $appends = [
'full_name'
];
// protected static function booted()
// {
// static::creating(function ($user) {
// if (empty($user->uuid)) {
// $user->uuid = (string) Str::uuid();
// }
// });
// }
public function getFullNameAttribute()
{
return $this->first_name . ' ' . $this->last_name;
}
public function hasVerifiedMobile(): bool
{
return !is_null($this->mobile_verified_at);
}
public function getAvatarAttribute()
{
return isset($this->attributes['avatar']) ? url($this->attributes['avatar']) : null;
}
public function setPasswordAttribute($password)
{
$this->attributes['password'] = Hash::make($password);
}
public function otpTokens()
{
return $this->hasMany(OtpTokens::class);
}
public function referrer()
{
return $this->belongsTo(User::class, 'referred_by');
}
public function referrals()
{
return $this->hasMany(User::class, 'referred_by');
}
public function transactions()
{
return $this->hasMany(Transaction::class);
}
public function reminders()
{
return $this->hasMany(Reminder::class);
}
public function packageNames()
{
return $this->belongsToMany(PackageName::class, 'user_package_name')->withPivot(['id', 'tries', 'source', 'fcm_token'])->withTimestamps();
}
public function products()
{
return $this->belongsToMany(Product::class, 'user_product')->withPivot(['expire_at', 'purchase_token', 'gateway'])->withTimestamps();
}
protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}
}