i-neop / laravel-file-upload
laravel file upload is package that provides to you upload single file or multi files
Requires
- php: ^7.4|^8.0.2
- illuminate/contracts: ^8.0|^9.0
- intervention/image: ^2.7
- spatie/laravel-medialibrary: ^9.0|^10.0
Requires (Dev)
- phpunit/phpunit: ^9.5.10
This package is auto-updated.
Last update: 2024-04-28 16:02:11 UTC
README
Installation
composer require i-neop/laravel-file-upload
open your Laravel config file config/app.php and add the following lines.
In the $providers array add the service providers for this package.
Intervention\Image\ImageServiceProvider::class
Add the facade of this package to the $aliases array.
'Image' => Intervention\Image\Facades\Image::class
publish media library migrations
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="migrations"
php artisan migrate
publish config
php artisan vendor:publish --provider="INeop\FileUpload\Providers\FileUploadServiceProvider" --tag="file-upload-config"
1- FileUploadService class
this class store file and return file name to store it in your model.
- it provide hash name for file
- it provide auto resize image
- it provide all methods in intervention image package.
- it provide config file to set max width, max height and quality.
Here are a few short examples of what you can do:
$post = new Post(); //... $post->image = FileUpload::make(request('image'))->store(); $post->save();
You can add path and disk, by default disk is public.
$post = new Post(); //... $post->image = FileUpload::make(request('image'))->store('posts', 's3'); $post->save();
You can use all methods in intervention image package.
$post = new Post(); //... $post->image = FileUpload::make(request('image')) ->resize(400, 400) ->crop(100, 100, 25, 25) ->store('posts', 's3'); $post->save();
You can use delete old file, for example in update.
$post = Post::find(1); //... $post->image = FileUpload::make(request('image')) ->delete($post->image) ->store('posts'); $post->save();
You can get path.
$post = new Post(); //... $fileUpload = FileUpload::make(request('image')); $post->image = $fileUpload->store('posts'); $filePath = $fileUpload->getFilePath(); $post->save();
Note: you can clean code by Accessors & Mutators
$post = Post::create([ //... ]); //In Post Model public function setImageAttribute($image) { $this->attributes['image'] = FileUpload::make($image)->store('posts'); }
To get image.
//In Post Model public function getImgAttribute() { return $this->image ? asset('storage/'. $this->image) : asset('images/post.jpg'); }
Dont forget run this command.
php artisan storage:link
2- MediaUploadService class
this class use media-library package to store media files for your model.
- it provide all methods in intervention image package.
- it provide all methods in media-library package.
- it provide upload multiple files.
- it provide config file to set max width, max height and quality.
To associate media with a model, the model must implement the following interface and trait:
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Spatie\MediaLibrary\HasMedia; use Spatie\MediaLibrary\InteractsWithMedia; class YourModel extends Model implements HasMedia { use InteractsWithMedia; }
Here are a few short examples of what you can do:
$post = new Post(); //... $post->save(); MediaUpload::make(request('image'))->setModel($post)->store();
You can add collection and disk, by default disk is public.
$post = new Post(); //... $post->save(); MediaUpload::make(request('image'))->setModel($post)->store('image', 's3');
You can upload multiple files.
$post = new Post(); //... $post->save(); MediaUpload::make(request('files'))->setModel($post)->store('images');
You can use all methods in intervention image package.
$post = new Post(); //... $post->save(); MediaUpload::make(request('image')) ->resize(500, 200) ->crop(100, 100, 25, 25) ->setModel($post) ->store('image', 's3');
You can use all methods in media library package.
$post = new Post(); //... $post->save(); MediaUpload::make(request('image')) ->resize(500, 200) ->setModel($post) ->usingName('my-image-name') ->withCustomProperties([ 'primaryColor' => 'red', 'image-code' => '12458558', ]) ->store('image');
To retrieve files you can use the getMedia-method:
$mediaItems = $yourModel->getMedia();
the getFirstMedia and getFirstMediaUrl convenience-methods are also provided:
$media = $yourModel->getFirstMedia(); $url = $yourModel->getFirstMediaUrl();
If you want to remove all associated media in a specific collection you can use the clearMediaCollection method. It also accepts the collection name as an optional parameter:
$yourModel->clearMediaCollection(); // all media will be deleted $yourModel->clearMediaCollection('images'); // all media in the images collection will be deleted
recommend: read Laravel-media library documentation.