tahirrasheed208 / laravel-medialibrary
Media Library for Laravel
Requires
- php: ^8.2
- ext-exif: *
- ext-fileinfo: *
- ext-json: *
- illuminate/database: ^11.0
- illuminate/support: ^11.0
- illuminate/view: ^11.0
- intervention/image-laravel: ^1.3
Requires (Dev)
- ext-imagick: *
- orchestra/testbench: ^9.0
- phpunit/phpunit: ^11.0
README
This package can associate images with Eloquent models.
Getting Started
1. Install
Run the following command:
composer require tahirrasheed208/laravel-medialibrary
2. Publish
Publish config file.
php artisan vendor:publish --provider="TahirRasheed\MediaLibrary\MediaLibraryServiceProvider" --tag=medialibrary-config
3. Preparing the database
You need to publish the migration to create the media table:
php artisan vendor:publish --provider="TahirRasheed\MediaLibrary\MediaLibraryServiceProvider" --tag=medialibrary-migration
After that, you need to run migrations.
php artisan migrate
Usage
Your Eloquent models should use the TahirRasheed\MediaLibrary\Traits\HasMedia
trait.
Use blade component to add file uploader in your form.
<x-medialibrary-file-upload name="image" />
For display old image in edit page.
<x-medialibrary-file-upload name="image" :model="$model" />
Upload
$model = Model::find(1); $model->handleMediaFromRequest()->toMediaCollection();
If your file input name is not image
then define second param.
$model->handleMediaFromRequest('banner')->toMediaCollection();
Upload to specific collection.
$model->handleMediaFromRequest()->toMediaCollection('images');
You can define default collection at eloquent level. Add below function in your model.
public function defaultCollection(): string { return 'post_images'; }
Upload to specific disk.
$model->handleMediaFromRequest()->useDisk('s3')->toMediaCollection();
Register Media Conversions
public function registerMediaConversions() { $this->addMediaConversion('post_main') ->width(420) ->height(350); }
You can register as many media conversions as you want
public function registerMediaConversions() { $this->addMediaConversion('post_main') ->width(420) ->height(350); $this->addMediaConversion('post_detail') ->width(700) ->height(550); }
Default force crop is disabled, but you can enable it
$this->addMediaConversion('post_main') ->width(420) ->height(350) ->crop();
Disable Conversions
If you want to disable registered conversions on some files
$model->handleMediaFromRequest()->withoutConversions()->toMediaCollection();
Configuration
Define your layout stack in config file.
'stack' => 'footer',
Or you can use our blade directive.
@mediaLibraryScript
Gallery with Dropzone
<x-medialibrary-dropzone name="gallery" />
Attach gallery to model using blelow code.
$model->attachGalleryToModelFromRequest('gallery')->toMediaCollection();
You can also define collection for gallery.
<x-medialibrary-dropzone name="gallery" collection="dropzone" />
You can define model to dropzone component as well. When you define model to component all images are automatically attached to model.
<x-medialibrary-dropzone name="gallery" :model="$model" />
You can also change the default dropzone message.
<x-medialibrary-dropzone name="gallery" message="Drop files here" />
Add Media from Url
$model->addMediaFromUrl($url, 'image')->toMediaCollection();
Implements with Laravel Settings
Install settings package
composer require tahirrasheed208/laravel-settings
Blade component to display old file
<x-medialibrary-file-upload name="image" setting="{{ setting()->get('name') }}" />
To upload file
setting()->upload($request->toArray(), 'file_name');
By default we expect file name is your option name, but you can define your option name as well
setting()->upload($request->toArray(), 'file_name', 'option_name');
Get Uploaded File Url
setting()->getFile('name');
Changelog
Please see Releases for more information what has changed recently.
Contributing
Pull requests are more than welcome. You must follow the PSR coding standards.
Security
If you discover any security related issues, please email tahirrasheedhtr@gmail.com instead of using the issue tracker.
License
The MIT License (MIT). Please see LICENSE for more information.