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

<?php

namespace App\Models;

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

use App\Helpers\Helper;

class ProductFile extends Model
{
    use HasFactory;

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

    protected $appends = ['product_file_id', 'product_file_unique_id'];

    protected $guarded = ['id'];

    public function getProductFileIdAttribute() { 

        return $this->id;
    }

    public function getProductFileUniqueIdAttribute() {

        return $this->unique_id;
    }

    public function product() {

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

    public static function boot() {

        parent::boot();

        static::creating(function ($model) {

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

        });

        static::created(function($model) {

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

            $model->save();
        
        });

        static::deleting(function ($model) {

            Helper::storage_delete_file($model->file, PRODUCT_FILE_PATH);
            
        });
    }
}
Back to Directory File Manager