"boolean", "gold" => "float", "is_archive" => "boolean", "finger_print" => "boolean", ]; static $TYPE = [ 1=>'همکار', 2=>'خانگی', 3=>'سرمایه گذار' ]; static $TYPES = [ 1=>'همکار', 2=>'خانگی', 3=>'سرمایه گذار' ]; public function setNameAttribute($value = null) { $fname = $this->attributes['fname'] ?? ''; $lname = $this->attributes['lname'] ?? ''; $this->attributes['name'] = trim("$fname $lname"); } public function wallet() { return $this->hasMany(Wallet::class ,'owner')->with(['fund','product'])->select(['cash', 'fund_id','product_id','gold', 'owner']); } public function orders() { return $this->hasMany(Order::class, 'customer_id'); } public function user() { return $this->belongsTo(User::class)->select(['id','name']); } public function referral() { return $this->belongsTo(Customer::class, 'referral_to_id', 'referral_id'); } /** معرف واقعی — با جستجوی صریح و جلوگیری از برگشت خود کاربر */ public function referrer(): ?self { if (empty($this->referral_to_id)) { return null; } return static::query() ->where('referral_id', $this->referral_to_id) ->where('id', '!=', $this->id) ->first(); } // hash password public function setPasswordAttribute($value){ $this->attributes['password'] = Hash::make($value); } public function getJWTIdentifier() { return $this->getKey(); } public function getJWTCustomClaims() { return []; } public function tahesab() { return $this->hasMany(Tahesab::class, 'customer_id', 'id'); } public function customer_settings() { return $this->hasMany(CustomersSetting::class, 'agent_id'); } public function package() { return $this->belongsTo(Package::class); } public function agent() { return $this->belongsTo(Customer::class, 'agent_id'); } protected static function booted(): void { static::saving(function (self $customer) { $mobile = self::normalizeMobile($customer->mobile); if ($mobile === null) { return; } $customer->mobile = $mobile; self::assertMobileUnique($mobile, $customer->exists ? (int) $customer->id : null); }); } public static function normalizeMobile(?string $mobile): ?string { $mobile = trim((string) $mobile); return $mobile === '' ? null : $mobile; } public static function isMobileTaken(?string $mobile, ?int $exceptId = null): bool { $mobile = self::normalizeMobile($mobile); if ($mobile === null) { return false; } $query = static::query() ->where('mobile', $mobile) ->whereNull('deleted_at'); if ($exceptId !== null) { $query->where('id', '<>', $exceptId); } return $query->exists(); } public static function assertMobileUnique(?string $mobile, ?int $exceptId = null): void { if (self::isMobileTaken($mobile, $exceptId)) { throw new UnprocessableEntityHttpException('این شماره موبایل قبلاً ثبت شده است'); } } }