Created
January 3, 2017 01:03
-
-
Save omarkdev/e5f0b5f234e4ca381402c62e831c3a1d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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