File "OrderItems.php"

Full Path: /home/clickysoft/public_html/jmapi5.clickysoft.net/app/Models/OrderItems.php
File size: 2.36 KB
MIME-type: text/x-php
Charset: utf-8

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;

class OrderItems extends Model
{
    use HasFactory;

    protected $table = 'order_items';
    protected $guarded = ['_token'];

    public function order()
    {
        return $this->belongsTo(Order::class);
    }

    public function purchaseOrderItems()
    {
        return $this->hasMany(PurchaseOrderItem::class, 'order_item_id');
    }

    protected function getTemplateMediaAttribute()
    {
        return $this->template
            ? asset('storage/order/' . $this->template)
            : '';
    }

    protected function getTemplateUrlAttribute()
    {
        return $this->template
            ? public_path('storage/order/' . $this->template)
            : '';
    }

    protected function getImageUrlAttribute()
    {
        return $this->image
            ? asset('storage/order/' . $this->image)
            : '';
    }

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

    public function productPrice()
    {
        return $this->belongsTo(ProductPrice::class, 'price_id');
    }

    public function itemVariations()
    {
        return $this->hasMany(OrderItemVariation::class, 'order_id');
    }

    public function orderItemVariations()
    {
        return $this->hasMany(OrderItemVariation::class, 'order_item_id');
    }

    public function orderItemCustomizations()
    {
        return $this->hasMany(OrderItemCustomizationData::class, 'order_item_id');
    }

    public function customizationSvg()
    {
        return $this->hasMany(OrderItemCustomizationSvg::class, 'order_item_id', 'id');
    }

    protected function getInvoicePathAttribute()
    {
        return $this->invoice_number
            ? public_path('storage/order/' . $this->pdf)
            : '';
    }

    public static function boot()
    {
        parent::boot();

        static::deleting(function ($order_item) {
            if ($order_item->attachment) {
                if (Storage::disk('order')->exists($order_item->template)) {
                    Storage::disk('order')->delete($order_item->template);
                }
            }

            foreach ($order_item->itemVariations as $variation) {
                $variation->delete();
            }
        });
    }
}