Skip to content

Instantly share code, notes, and snippets.

@omarkdev
Created January 3, 2017 01:03
Show Gist options
  • Save omarkdev/e5f0b5f234e4ca381402c62e831c3a1d to your computer and use it in GitHub Desktop.
Save omarkdev/e5f0b5f234e4ca381402c62e831c3a1d to your computer and use it in GitHub Desktop.
<?php
namespace Smart\Traits\Models;
use File;
use Illuminate\Http\UploadedFile;
trait CoverTrait{
/**
* Upload image when image is set
*
* @param $image
*
* @return string
*/
public function setCoverAttribute($image){
if($image === null)
return $this->attributes['cover'] = '';
if(!$image instanceof UploadedFile)
return $this->attributes['image'] = $image;
$nameImage = (str_random(12) . time()).".".$image->getClientOriginalExtension();
$image->move(public_path().$this->pathUpload, $nameImage);
return $this->attributes['cover'] = $nameImage;
}
/**
* Return image name with pathUpload
*
* @param $value
*
* @return string
*/
public function getCoverAttribute($value){
return $this->pathUpload.$value;
}
/**
* Return image without pathUpload
*
* @param $value
*
* @return mixed
*/
public function getCoverNoPathAttribute($value){
return str_replace($this->pathUpload, "", $this->image);
}
/**
* Delete image from path
*
* @return bool
*/
public function deleteCover(){
if($this->defaultImage == $this->image_no_path)
return false;
File::delete(public_path($this->image));
return true;
}
/**
* Define default image in user image
*/
public function putDefaultImage()
{
$this->attributes['image'] = $this->defaultImage;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment