Viewing File: /home/ubuntu/vedadeals-backend-base/app/Models/UserWallet.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class UserWallet extends Model
{
    use HasFactory;

    protected $hidden = ['id', 'unique_id'];

    protected $appends = ['user_wallet_id', 'user_wallet_unique_id', 'total_formatted', 'onhold_formatted', 'remaining_formatted', 'used_formatted'];

    protected $guarded = ['id'];

    public function getUserWalletIdAttribute() { 

        return $this->id;
    }

    public function getUserWalletUniqueIdAttribute() {

        return $this->unique_id;
    }

    public function user() {

        return $this->belongsTo(User::class)->withDefault();
    }

    public function getTotalFormattedAttribute() {

        return formatted_amount($this->total);
    }

    public function getOnholdFormattedAttribute() {

        return formatted_amount($this->onhold);
    }

    public function getRemainingFormattedAttribute() {

        return formatted_amount($this->remaining);
    }

    public function getUsedFormattedAttribute() {

        return formatted_amount($this->used);
    }

    public static function boot() {

        parent::boot();

        static::creating(function ($model) {

            $model->attributes['unique_id'] = "UW-".uniqid();

        });

        static::created(function($model) {

            $model->attributes['unique_id'] = "UW-".$model->attributes['id']."-".uniqid();
            
            $model->save();
        
        });
    }
}
Back to Directory File Manager