Edit File: AuthBaseModel.php
<?php namespace App\Models; use App\Mail\SendCode; use App\Services\Sms\SmsService; use App\Traits\SmsTrait; use App\Traits\UploadTrait; use Carbon\Carbon; use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Illuminate\Support\Facades\Mail; use Laravel\Sanctum\HasApiTokens; class AuthBaseModel extends Authenticatable { use Notifiable, UploadTrait, HasApiTokens, SmsTrait, SoftDeletes; const IMAGEPATH = 'users'; public function scopeSearch($query, $searchArray = []) { $query->where(function ($query) use ($searchArray) { if ($searchArray) { foreach ($searchArray as $key => $value) { if (str_contains($key, '_id')) { if (null != $value) { $query->Where($key, $value); } } elseif ('order' == $key) { } elseif ('created_at_min' == $key) { if (null != $value) { $query->WhereDate('created_at', '>=', Carbon::createFromFormat('m-d-Y', $value)); } } elseif ('created_at_max' == $key) { if (null != $value) { $query->WhereDate('created_at', '<=', Carbon::createFromFormat('m-d-Y', $value)); } } else { if (null != $value) { $query->Where($key, 'like', '%' . $value . '%'); } } } } }); return $query->orderBy('id', request()->searchArray && request()->searchArray['order'] ? request()->searchArray['order'] : 'DESC'); } public function setPhoneAttribute($value) { if (!empty($value)) { $this->attributes['phone'] = fixPhone($value); } } public function setCountryCodeAttribute($value) { if (!empty($value)) { $this->attributes['country_code'] = fixPhone($value); } } public function getFullPhoneAttribute() { return $this->attributes['country_code'] . $this->attributes['phone']; } public function getImageAttribute() { if ($this->attributes['image']) { $image = $this->getImage($this->attributes['image'], static::IMAGEPATH); } else { $image = $this->defaultImage('users'); } return $image; } public function setImageAttribute($value) { if (null != $value && is_file($value)) { isset($this->attributes['image']) ? $this->deleteFile($this->attributes['image'], static::IMAGEPATH) : ''; $this->attributes['image'] = $this->uploadAllTyps($value, static::IMAGEPATH); } } public function setPasswordAttribute($value) { if ($value) { $this->attributes['password'] = bcrypt($value); } } public function notifications() { return $this->morphMany(Notification::class, 'notifiable')->orderBy('created_at', 'desc'); } public function markAsActive() { $this->update(['code' => null, 'code_expire' => null, 'active' => true]); return $this; } public function sendVerificationCode(): array { $this->update([ 'code' => $this->activationCode(), 'code_expire' => Carbon::now()->addMinute(), ]); $this->sendCodeAtSms($this->code); return ['user' => $this]; } private function activationCode(): int { return 123456; // return mt_rand(111111, 999999); } public function sendCodeAtSms($code, $full_phone = null): void { (new SmsService())->sendSms($full_phone ?? $this->full_phone, trans('api.activeCode') . $code); } public function sendCodeAtEmail($code, $email = null): void { try { Mail::to($email ?? $this->email)->send(new SendCode($code, $this->name)); } catch (\Exception $e) { info('Failed to send email: ' . $e->getMessage()); } } public function devices() { return $this->morphMany(Device::class, 'morph'); } public function login() { // $this->tokens()->delete(); $this->updateDevice(); $this->updateLang(); $token = $this->createToken(request()->device_type)->plainTextToken; return $token; } public function updateLang() { if ( request()->header('Lang') != null && in_array(request()->header('Lang'), languages()) ) { $this->update(['lang' => request()->header('Lang')]); } else { $this->update(['lang' => defaultLang()]); } } public function updateDevice() { if (request()->device_id) { $this->devices()->updateOrCreate([ 'device_id' => request()->device_id, 'device_type' => request()->device_type, 'voip_id' => request()->voip_id ]); } } public function logout() { // $this->tokens()->delete(); $this->currentAccessToken()->delete(); if (request()->device_id) { $this->devices()->where(['device_id' => request()->device_id])->delete(); } return true; } public function rooms() { return $this->morphMany(RoomMember::class, 'memberable'); } public function ownRooms() { return $this->morphMany(Room::class, 'createable'); } public function joinedRooms() { return $this->morphMany(RoomMember::class, 'memberable') ->with('room') ->get() ->sortByDesc('room.last_message_id') ->pluck('room'); } public function replays() { return $this->morphMany(ComplaintReplay::class, 'replayer'); } public function authUpdates() { return $this->morphMany(AuthUpdate::class, 'updatable'); } public function settlements() { return $this->morphMany(Settlement::class, 'transactionable')->latest(); } public function wallet() { return $this->morphOne(Wallet::class, 'walletable')->latest(); } public function complaints(): MorphMany { return $this->morphMany(Complaint::class, 'complaintable'); } public function rates() { // sent to others return $this->morphMany(Rate::class, 'ratingable'); } public function givenRates() { return $this->morphMany(Rate::class, 'ratedable'); } public static function boot() { parent::boot(); /* creating, created, updating, updated, deleting, deleted, forceDeleted, restored */ static::deleted(function ($model) { $model->deleteFile($model->attributes['image'], self::IMAGEPATH); }); static::created(function ($model) { $model->wallet()->create(); }); } }
Back to File Manager