File "Cart.php"

Full Path: /home/clickysoft/public_html/jmapi5.clickysoft.net/app/Models/Cart.php
File size: 1.33 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 Cart extends Model
{
    use HasFactory;

    protected $casts = [
        'created_at'  => 'date:m-d-Y',
        'updated_at'  => 'date:m-d-Y',
    ];

    protected $fillable = [
        'user_id',
        'product_id',
        'price_id',
        'customization',
        'attachment',
        'quantity',
        'created_at',
        'updated_at',
    ];

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

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

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

    public function items()
    {
        return $this->hasMany(CartItem::class, 'cart_id');
    }

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

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

}