Viewing File: /home/ubuntu/vedadeals-backend-base/app/Models/User.php
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use App\Helpers\Helper;
use Hash, Setting;
use Carbon\Carbon;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $guarded = ['picture', 'cover', 'token', 'token_expiry'];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [ 'password', 'remember_token', 'id', 'dob' ];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [ 'email_verified_at' => 'datetime'];
protected $appends = ['user_id', 'user_unique_id', 'dob_formatted', 'age'];
public function getUserIdAttribute() {
return $this->id;
}
public function getUserUniqueIdAttribute() {
return $this->unique_id;
}
public function scopeApproved($query) {
return $this->where('status', APPROVED);
}
public function userWallet() {
return $this->hasOne(UserWallet::class);
}
public function carts() {
return $this->hasMany(Cart::class);
}
public function userWithdrawals() {
return $this->hasMany(UserWithdrawal::class);
}
public function walletTransactions() {
return $this->hasMany(UserWalletPayment::class);
}
public function wishlists() {
return $this->hasMany(Wishlist::class);
}
public function orders() {
return $this->hasMany(Order::class);
}
public function getDobFormattedAttribute() {
return common_date($this->dob, '', 'd M Y');
}
public function getAgeAttribute() {
return Carbon::parse($this->dob)->diffInYears();
}
public function bellNotifications() {
return $this->hasMany(BellNotification::class, 'to_user_id');
}
public function unReadBellNotifications() {
return $this->bellNotifications()->where(['is_seen' => NO]);
}
public static function boot() {
parent::boot();
static::creating(function ($model) {
$model->attributes['unique_id'] = uniqid();
$model->attributes['username'] = routefreestring(strtolower($model->attributes['name'] ?: rand(1,10000).rand(1,10000))).'-'.$model->attributes['unique_id'];
$model->attributes['token'] = Helper::generate_token();
$model->attributes['token_expiry'] = Helper::generate_token_expiry();
$model->attributes['status'] = USER_APPROVED;
if (Setting::get('is_account_email_verification') == YES && envfile('MAIL_USERNAME') && envfile('MAIL_PASSWORD') && $model->attributes['login_by'] == MANUAL) {
$model->generateEmailCode();
} else {
$model->attributes['is_email_verified'] = USER_EMAIL_VERIFIED;
}
$model->attributes['password'] = Hash::make($model->attributes['password']);
});
static::created(function($model) {
$model->attributes['unique_id'] = routefreestring(strtolower($model->attributes['username'] ?: rand(1,10000).rand(1,10000)));
$model->save();
UserWallet::Create(['user_id' => $model->attributes['id']]);
});
static::updating(function($model) {
$model->attributes['unique_id'] = routefreestring(strtolower($model->attributes['username'] ?: rand(1,10000).rand(1,10000)));
});
static::deleting(function ($model) {
Helper::storage_delete_file($model->picture, PROFILE_PATH_USER);
Helper::storage_delete_file($model->cover, PROFILE_PATH_USER);
});
}
/**
* Generates Token and Token Expiry
*
* @return bool returns true if successful. false on failure.
*/
protected function generateEmailCode() {
$this->attributes['verification_code'] = Helper::generate_email_code();
$this->attributes['verification_code_expiry'] = Helper::generate_email_expiry();
return true;
}
}
Back to Directory
File Manager