Edit File: BaseModel.php
<?php namespace App\Models; use Carbon\Carbon; use App\Traits\UploadTrait; use Illuminate\Database\Eloquent\Model; class BaseModel extends Model { use UploadTrait; public function scopeSearch($query, $searchArray = []) { $query->where(function ($query) use ($searchArray) { if ($searchArray) { foreach ($searchArray as $key => $value) { if (str_contains($key, '_id')) { if ($value != null) { $query->Where($key , $value); } }elseif ($key == 'order' ) { }elseif ($key == 'created_at_min' ) { if ($value != null ) { $query->WhereDate('created_at' , '>=' , Carbon::createFromFormat('m-d-Y', $value)); } }elseif ($key == 'created_at_max') { if ($value != null ) { $query->WhereDate('created_at' , '<=' , Carbon::createFromFormat('m-d-Y', $value)); } }else{ if ($value != null ) { $query->Where($key, 'like', '%'.$value.'%'); } } } } }); return $query->orderBy('created_at' , request()->searchArray && request()->searchArray['order'] ? request()->searchArray['order'] : 'DESC' ); } public function getImageAttribute() { if ($this->attributes['image']) { $image = $this->getImage($this->attributes['image'], static::IMAGEPATH); } else { $image = $this->defaultImage( static::IMAGEPATH); } return $image; } public function setImageAttribute($value) { if (null != $value && is_file($value) ) { isset($this->attributes['image']) ? $this->deleteFile($this->attributes['image'] , static::IMAGEPATH) : ''; $this->attributes['image'] = $this->uploadAllTyps($value, static::IMAGEPATH); } } protected function asJson($value) { return json_encode($value, JSON_UNESCAPED_UNICODE); } public static function boot() { parent::boot(); /* creating, created, updating, updated, deleting, deleted, forceDeleted, restored */ static::deleted(function($model) { if (!in_array(get_class($model), [ProviderUpdate::class,DelegateUpdate::class])){ if (isset($model->attributes['image'])) { $model->deleteFile($model->attributes['image'], static::IMAGEPATH); } } }); } }
Back to File Manager