Has-Images is a package that provides a quick and easy way to link images with a model through images key for Laravel.
This package depends on Uploader.
Install the latest version using Composer.
1composer require pharaonic/laravel-has-images2php artisan migrate
Images uploading is gonna be easiest thing you will think about, so you will be able to store, retrieve, update and delete images so easily.
You have to include HasImages trait into your model first.
Optional : You can customize the uploading options through Uploader.
1namespace App\Models; 2 3use Illuminate\Database\Eloquent\Model; 4use Pharaonic\Laravel\Images\HasImages; 5 6class Article extends Model 7{ 8 use HasImages; 9 10 protected $filesOptions = [
11 'images' => [12 'directory' => '/articles/images',13 'thumbnail' => [14 'ratio' => true,15 'width' => 400,16 'height' => null17 ]18 ],19 ];20}
This is how you can store images into your model, you can do that on create or add a single image to your model or even replace all the images with a new list of images.
1// On Create 2$article = Article::create([ 3 'images' => [$request->file1, $request->file2, $request->file3] 4]); 5 6// Add Image 7$article->addImage($request->file1); 8 9// Replace All Images 10$article->images = [$request->file1, $request->file2, $request->file3];11$article->save();
It's an array of images so you can retrieve it easily or it's thumbnails too.
1echo $article->images[0]->url;2echo $article->images[0]->thumbnail->url;
You can delete a single image or all images or the model with the images too.
1// Delete Single Image 2$article->images[2]->delete();3 4// Delete All Images 5$article->clearImages();6 7// Delete Model With It's Images too 8$article->delete();
You can publish the migrations.
1php artisan vendor:publish --tag=laravel-has-images