hasMany(SocialProvider::class); } public function avatar(): BelongsTo { return $this->belongsTo(Asset::class, 'asset_id'); } public function getInitialsAttribute(): string { return collect(explode(' ', $this->name))->slice(0, 2)->map(function($item) { return Str::upper(Str::substr($item, 0, 1)); })->join(''); } public function getFirstAndMidNameAttribute(): string { return collect(explode(' ', $this->name))->slice(1, 2)->join(' '); } public function getIsAdminAttribute(): bool { return $this->hasRole('Administrator'); } public static function getByData($data, $triggerEvent = false) { $result = false; if ($email = trim($data['email'] ?? null)) { $result = self::query()->where(['email' => $email])->first(); if (!$result) { $password = $data['password'] ?? (App::environment('local') ? 'Qwerty1!' : Str::random(8)); $result = self::create(['email' => $email, 'password' => $password]); $result->update(['name' => trim($data['name'] ?? null), 'phone' => trim($data['phone'] ?? null)]); $result->assignRole($data['role'] ?? 'User'); if ($triggerEvent) event(new UserRegistered($result, $password)); } else { if ($val = trim($data['phone'] ?? null)) $result->update(['phone' => $val]); } if ($val = $data['avatar'] ?? null) $result->setAvatar($val); } return $result; } public function setAvatar($val) { $asset = Asset::byUuid($val)->first(); $this->update(['asset_id' => $asset->id ?? null]); } public static function create(array $attributes = []) { if (array_key_exists('password', $attributes)) { $attributes['password'] = Hash::make($attributes['password']); } return static::query()->create($attributes); } public function sendPasswordResetNotification($token) { Mail::to($this->email)->send(new PasswordResetRequested($this, $token)); } }