ImageTrait for laravel. Upload/Delete image and thumb image, store path in DB
yashenkov/laravel-imagetrait is a Laravel package for imagetrait for laravel. upload/delete image and thumb image, store path in db.
It currently has 1 GitHub stars and 16 downloads on Packagist.
Install it with composer require yashenkov/laravel-imagetrait.
Discover more Laravel packages by yashenkov
or browse all Laravel packages to compare alternatives.
Last updated
for Hacktoberfest 2017
ImageTrait for Laravel 5.4
Handle image file upload and DB storage for a image file:
composer require yashenkov/laravel-imagetrait
Download ImageTrait.php, than place it in app/Traits.
Replace namespase "Yashenkov\ImageTrait\Traits" by "App\Traits"
In your model, that have image attribute you need to add in Model use ImageTrait, for example:
<?php
namespace App\Models;
use App\Traits\ImageTrait;
use Illuminate\Database\Eloquent\Model;
class Product extends Model {
use ImageTrait;
...
}
Then you can use method of the trait in Model class as mutator of image attribute:
<?php
namespace App\Models;
use App\Traits\ImageTrait;
use Illuminate\Database\Eloquent\Model;
class Product extends Model {
use ImageTrait;
public $disk = "uploads";
public $destination_path = "products";
public $imageWidth = 270;
public $imageHeight = 270;
/**
* Mutators
*/
public function setImageAttribute($value, $attribute_name = 'image')
{
$this->uploadImageToDisk($value, $attribute_name, $this->disk, $this->destination_path, $this->imageWidth, $this->imageHeight);
}
}
##Licence MIT