Viewing File: /home/ubuntu/shop-website-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 = ['deleted_at', 'id', 'unique_id'];

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

    public function getOrderProductIdAttribute() {

        return $this->id;
    }

    public function getOrderProductUniqueIdAttribute() {

        return $this->unique_id;
    }

    public function getTotalFormattedAttribute() {

        return formatted_amount($this->total);
    }

    public function productDetails() {

        return $this->belongsTo(Product::class,'product_id');
    }


    public function orders()
    {
        return $this->belongsTo(Order::class,'order_id');
    }

    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::deleting(function ($model){


        });
    }

}
Back to Directory File Manager