mar3y / image-upload
A package for handling image uploads in Laravel applications.
Installs: 35
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/mar3y/image-upload
Requires
- php: ^7.4|^8.0|^8.1|^8.2|^8.3
- illuminate/database: ^8.0|^9.0|^10.0|^11.0|^12.0
- illuminate/filesystem: ^8.0|^9.0|^10.0|^11.0|^12.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^6.0|^7.0|^8.0|^9.0
- phpunit/phpunit: ^9.0|^10.0|^11.0
README
A Laravel package for handling image uploads in your applications.
Installation
You can install the package via composer:
composer require mar3y/image-upload
Usage
Using the HasImage Trait
To use the HasImage trait in your model, follow these steps:
-
Add the trait to your model class:
use mar3y\ImageUpload\Traits\HasImage; class YourModel extends Model { use HasImage; protected static $imageAttributes = ['image']; }
-
Define the image attributes:
In your model class, define the
$imageAttributesproperty as an array of attribute names that will store image paths. For example, if your model has animageattribute, you would setprotected static $imageAttributes = ['image'];. -
Upload an image:
When you save a model instance with an image attribute set to an
UploadedFileinstance, the trait will automatically upload the image and store the path in the attribute.$model = new YourModel(); $model->image = $request->file('image'); $model->save();
-
Retrieve the image URL:
You can retrieve the full URL of the image using the attribute name.
$imageUrl = $model->image;
Using the ImageUploadHelper
You can also use the ImageUploadHelper class directly:
use mar3y\ImageUpload\Helpers\ImageUploadHelper; $path = ImageUploadHelper::uploadImage($file, 'your-directory');
License
The MIT License (MIT). Please see License File for more information.