Viewing File: /home/ubuntu/vedadeals-backend-base/app/Models/SubCategory.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Helpers\Helper;
class SubCategory extends Model
{
use HasFactory;
protected $hidden = ['id','unique_id'];
protected $appends = ['sub_category_id', 'sub_category_unique_id', 'description_formatted'];
protected $guarded = ['id', 'picture'];
public function getSubCategoryIdAttribute() {
return $this->id;
}
public function getSubCategoryUniqueIdAttribute() {
return $this->unique_id;
}
public function getDescriptionFormattedAttribute() {
return strip_tags($this->description);
}
public function scopeApproved($query) {
return $query->where('status', APPROVED);
}
public function category() {
return $this->belongsTo(Category::class)->withDefault();
}
public function products() {
return $this->hasMany(Product::class);
}
public function availableProducts() {
return $this->hasMany(Product::class)->where(['visible_status' => YES, 'status' => APPROVED]);
}
public static function boot() {
parent::boot();
static::creating(function ($model) {
$model->attributes['unique_id'] = "SC-".uniqid();
});
static::created(function($model) {
$model->attributes['unique_id'] = routefreestring(strtolower($model->attributes['name']));
$model->save();
});
static::updating(function ($model) {
$model->attributes['unique_id'] = routefreestring(strtolower($model->attributes['name']));
});
static::deleting(function ($model) {
Helper::storage_delete_file($model->picture, SUB_CATEGORY_FILE_PATH);
});
}
}
Back to Directory
File Manager