Viewing File: /home/ubuntu/vedadeals-backend-base/app/Models/Product.php

<?php

namespace App\Models;

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

class Product extends Model
{
    use HasFactory, SoftDeletes;

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

    protected $appends = ['product_id', 'product_unique_id', 'description_formatted', 'price_formatted'];

    protected $guarded = [];

    public function getProductIdAttribute() { 

        return $this->id;
    }

    public function getProductUniqueIdAttribute() {

        return $this->unique_id;
    }

    public function getDescriptionFormattedAttribute() {

        return strip_tags($this->description);
    }

    public function getPriceFormattedAttribute() {

        return formatted_amount($this->price);
    }

    public function scopeApproved($query) {

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

    public function category() {

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

    public function subCategory() {

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

    public function defaultFile() {

        return $this->hasOne(ProductFile::class)->where(['is_default' => YES]);
    }

    public function productFiles() {

        return $this->hasMany(ProductFile::class);
    }

    public function scopeAvailable($query) {

        return $query->where(['visible_status' => YES, 'status' => APPROVED]);
    }

    public function productInventory() {

        return $this->hasOne(ProductInventory::class);
    }

    public function orders() {

        return $this->hasMany(Order::class);
    }

    public function orderProducts() {

        return $this->hasMany(OrderProduct::class);
    }

    public function discount() {

        return $this->hasOne(Discount::class)->where('is_current', YES)->withDefault();
    }

    public function discounts() {

        return $this->hasMany(Discount::class);
    }

    public function reviews() {

        return $this->hasMany(Review::class);
    }

    public function banner() {

        return $this->hasOne(Banner::class)->withDefault();
    }

    /**
     * @method generate_unique_id()
     *
     * @uses to check & generate unique id
     *
     * @created Karthick
     *
     * @updated
     *
     * @param 
     *
     * @return $unique_id
     *
     */
    protected function generate_unique_id() {

        $unique_id = routefreestring(strtolower($this->attributes['name'] ? : rand(1,10000).rand(1,10000)));

        $is_already_exists = Product::where([ ['id', '!=' , $this->attributes['id']], ['unique_id', $unique_id] ] )->withTrashed()->exists();

        return $is_already_exists ? $unique_id.'-'.$this->attributes['id'] : $unique_id;
    
    }

    public static function boot() {

        parent::boot();

        static::creating(function ($model) {

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

        });

        static::created(function($model) {

            $model->attributes['unique_id'] = $model->generate_unique_id();

            $model->save();

            ProductInventory::Create([
                'product_id' => $model->attributes['id'],
                'total' => $model->attributes['quantity'],
                'remaining' => $model->attributes['quantity'],
            ]);
        
        }); 

        static::updating(function ($model) {

            $model->attributes['unique_id'] = $model->generate_unique_id();

        });

        static::deleting(function ($model) {

            $model->banner->delete();
        });
    }
}
Back to Directory File Manager