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

<?php

namespace App\Models;

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

class Order extends Model
{
    use HasFactory;

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

    protected $appends = ['order_id', 'order_unique_id'];

    protected $guarded = ['id'];

    public function getOrderIdAttribute() { 

        return $this->id;
    }

    public function getOrderUniqueIdAttribute() {

        return $this->unique_id;
    }

    public function scopeApproved($query) {

        return $query->where('status', APPROVED);
    }

    public function deliveryAddress() {

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

    public function orderProducts() {

        return $this->hasMany(OrderProduct::class);
    }

    public function user() {

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

    public function orderPayment() {

        return $this->hasOne(OrderPayment::class)->withDefault();
    }

    public function orderTracking() {

        return $this->hasOne(OrderTracking::class)->withDefault();
    }

    public static function boot() {

        parent::boot();

        static::creating(function ($model) {

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

        });

        static::created(function($model) {

            $model->attributes['unique_id'] = "O-".str_pad($model->attributes['id'], 5, "0", STR_PAD_LEFT);

            $model->save();

            OrderTracking::Create([
                'order_id' => $model->attributes['id'],
                'user_id' => $model->attributes['user_id'],
                'received_at' => $model->attributes['created_at']
            ]);
        
        }); 

        static::updating(function ($model) {
   
        });

        static::deleting(function ($model) {
            
        });
    }
}
Back to Directory File Manager