Edit File: User.php
<?php namespace App\Models; use App\Traits\DeviceTrait; use App\Traits\ReportTrait; use App\Traits\SmsTrait; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Illuminate\Support\Facades\Mail; use Laravel\Passport\HasApiTokens; use App\Traits\Uploadable; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\File; use Illuminate\Database\Eloquent\SoftDeletes; class User extends Authenticatable { use HasApiTokens,SoftDeletes, HasFactory, SmsTrait, Notifiable,Uploadable, ReportTrait, DeviceTrait; protected $fillable = ['name' , 'name_en', 'points' , 'notify_sound', 'city_id' , 'lang' ,'email','country_key','phone','changed_phone','block','password','offers_notify','new_orders_notify','email_verified_at','phone_verified_at','avatar','status','active','gender','completed_info','type','code','pin_code','lat','long','address','wallet','total_bills','total_delivery_fees','num_orders','num_comments','num_rating','rate','company_id' , 'approve','bank_iban_number','nationality_id']; const ADMIN_ID = 1; const ADMIN_TYPE = 'admin'; protected $guarded = []; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', 'phone_verified_at' => 'datetime', ]; public function getFullPhoneAttribute() { return '0'.$this->phone; } public function scopeSearch($query, $searchArray = []) { $query->where(function ($query) use ($searchArray) { if($searchArray){ foreach ($searchArray as $key => $value) { if (null != $value) { if (str_contains($key, '_id')) { $query->Where($key, $value); } elseif ('created_at_min' == $key) { $query->WhereDate('created_at', '>=', $value); } elseif ('created_at_max' == $key) { $query->WhereDate('created_at', '<=', $value); } elseif('order' != $key) { $query->Where($key, 'like', '%' . $value . '%'); } } } } }); return $query->orderBy('created_at', request()->searchArray && request()->searchArray['order'] ? request()->searchArray['order'] : 'DESC'); } public function getFullChangedPhoneAttribute() { return '0'.$this->changed_phone; } public function getAvatarPathAttribute() { return $this->avatar?asset('assets/uploads/users/' . $this->avatar):asset('assets/uploads/users/default.png'); } public function setAvatarAttribute($value) { if($value && is_file($value)){ File::delete(public_path('assets/uploads/users/' . $this->avatar)); $this->attributes['avatar'] = $this->uploadFile($value, 'users', true, 250, null); } else { $this->attributes['avatar'] = $value; } } public static function boot() { parent::boot(); /* creating, created, updating, updated, deleting, deleted, forceDeleted, restored */ self::deleted(function ($model) { $model->deleteFile($model->attributes['avatar'], 'users'); }); } public function settlements() { return $this->hasMany(Settlement::class,'provider_id'); } public function waitingSettlementOrders() { return $this->hasMany(WaitingSettlementOrder::class,'provider_id'); } public function ordersInfo() { $totals = $this->waitingSettlementOrders() ->where('waiting_settlement_orders.status', 'pending') ->join('orders', 'orders.id', '=', 'waiting_settlement_orders.order_id') ->selectRaw(' SUM(orders.total_price) as total_price, SUM(orders.price) as price, SUM(orders.app_percentage) as app_percentage, SUM(orders.added_value) as added_value, SUM(orders.price - orders.app_percentage) as total_due ') ->first(); // Cast the result to an array return ['totals'=> ['total_price' => number_format($totals->total_price,2,'.','')??0, 'total_app_percentage' => number_format($totals->app_percentage,2,'.','')??0, 'total_added_value' => number_format($totals->added_value,2,'.','')??0, 'total_due' => number_format($totals->total_due,2,'.','')??0, ] ]; } public function payout() { return $this->belongsTo(Payout::class); } public function complain() { return $this->hasMany(Complain::class); } public function devices() { return $this->hasMany(userDevices::class); } public function reports() { return $this->hasMany(Report::class); } public function delegateJoinRequests() { return $this->hasMany(DelegateJoinrequest::class); } public function markAsActive() { $this->update(['status' => 'active' , 'code' => null]); } public function confirmChangePhone() { $this->update(['phone' => $this->changed_phone, 'changed_phone'=>NULL]); } public function userOrders() { return $this->hasMany(Order::class,'user_id'); } public function tickets() { return $this->hasMany(Ticket::class); } public function myReviews(){ return $this->hasMany(Review::class,'user_id'); } public function reviews(){ return $this->hasMany(Review::class,'seconduser_id'); } public function addresses(){ return $this->hasMany(Address::class); } public function rating(){ $reviewsCount = $this->reviews()->count(); if($reviewsCount == 0){ return number_format(0,2); } $reviews = $this->reviews; $sum = 0; foreach($reviews as $review){ $sum+=$review->rate; } return number_format(round($sum/$reviewsCount,2),2); } public function getFullSmsPhoneAttribute() { return $this->attributes['country_key'] . $this->attributes['phone']; } public function sendVerificationCode($has_changed_phone = 'false') { $package = SMS::where('active','1')->first(); $code = '1234'; $data['code'] = $code; $msg = trans('auth.yourcode').' : '.$code; $this->update(['code' => $code]); $this->sendSms($this->FullSmsPhone , $msg , $package->key , $package); } public function resetCode($password) { $this->update([ 'code' => null, 'password' => $password ]); } public function ads(){ return $this->belongsToMany(Ad::class,'user_ads')->withPivot('user_id'); } public function city() { return $this->belongsTo(City::class); } public function store() { return $this->hasOne(Store::class); } public function setPasswordAttribute($value) { if ($value != null) { $this->attributes['password'] = Hash::make($value); } } }
Back to File Manager