Edit File: Ads.php
<?php namespace App\Models; use App\Traits\Uploadable; use Illuminate\Database\Eloquent\Model; use Spatie\Translatable\HasTranslations; use Illuminate\Database\Eloquent\Factories\HasFactory; class Ads extends Model { use HasFactory, HasTranslations , Uploadable ; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = ['title','content', 'store_id','image','profit','cover','views' , 'status']; public $translatable = ['title','content']; protected $table = 'ads'; protected $guarded = []; protected function asJson($value) { return json_encode($value, JSON_UNESCAPED_UNICODE); } public function store(){ return $this->belongsTo(Store::class); } public function subscription(){ return $this->hasOne(Subscription::class , 'ads_id' , 'id'); } public function setImageAttribute($value) { if (is_file($value)) { $this->attributes['image'] = $this->uploadOne($value, 'ads'); } } public function setCoverAttribute($value) { if (is_file($value)) { $this->attributes['cover'] = $this->uploadFile($value, 'ads'); } } public function getImagePathAttribute() { if ($this->attributes['image']) { $image = $this->getImage($this->attributes['image'], 'ads'); } else { $image = $this->defaultImage('ads'); } return $image; } public function getCoverPathAttribute() { if ($this->attributes['cover']) { $image = $this->getImage($this->attributes['cover'], 'ads'); } else { $image = $this->defaultImage('ads'); } return $image; } public static function boot() { parent::boot(); /* creating, created, updating, updated, deleting, deleted, forceDeleted, restored */ self::deleted(function ($model) { $model->deleteFile($model->attributes['cover'], 'ads'); $model->deleteFile($model->attributes['image'], 'ads'); }); } }
Back to File Manager