<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;

class ServiceType extends Model
{
    Protected $fillable = ['title','image','narration'];


    public function imageUpload($Image,$ServiceType)
        {
        // dd($Image);
        $FileName=$Image->getClientoriginalName();  
        $FileName=time().$FileName;
        $this->imageDeleteIfExist($ServiceType);
        $Image->storeAS('images/services',$FileName,'public'); //Upload     
    $ServiceType->update(['image'=>$FileName]);      //Update Avatar Field
        }

    private  function imageDeleteIfExist($ServiceType)
        {
    
        if($ServiceType->image)
        {
        Storage::delete ('/public/images/services/'.$ServiceType->image);
        }
    }
}