Viewing File: /home/ubuntu/route-and-root-backend-base/app/Models/OrderProduct.php

<?php

namespace App\Models;

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

class OrderProduct extends Model
{
    use HasFactory;

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

    protected $appends = ['order_product_id', 'order_product_unique_id'];

    protected $guarded = ['id'];

    public function getOrderProductIdAttribute() { 

        return $this->id;
    }

    public function getOrderProductUniqueIdAttribute() {

        return $this->unique_id;
    }

    public function scopeApproved($query) {

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

    public function product() {

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

    public static function boot() {

        parent::boot();

        static::creating(function ($model) {

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

        });

        static::created(function($model) {

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

            $model->save();
        
        }); 

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

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