Spatie Laravel Medialibrary Pro is a powerful package that enhances media management in Laravel applications. It allows you to easily associate files with Eloquent models and provides a range of features for handling media files, including image manipulation, file conversions, and responsive images.
Key Features
- File Associations: Easily associate media files with Eloquent models.
- Image Manipulation: Automatically handle image processing and resizing.
- Conversions: Define conversions to create different formats of the same image.
- Responsive Images: Generate multiple image sizes for responsive design.
- Custom Properties: Store additional data related to media items.
- Versioning: Track changes and maintain multiple versions of media files.
- Easy Integration: Simple to integrate with existing Laravel projects.
Installation
To install Spatie Laravel Medialibrary Pro, run the following command:
1 |
composer require spatie/laravel-medialibrary-pro |
Configuration
After installing the package, publish the configuration file:
1 |
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" |
This command creates a config/medialibrary.php
configuration file that you can customize as needed.
Setting Up Your Model
To use the medialibrary in your models, you need to implement the InteractsWithMedia
trait from the package. Here’s an example of how to set it up in a Post
model:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
namespace App\Models; use Illuminate\Database\Eloquent\Model; use Spatie\MediaLibrary\HasMedia; use Spatie\MediaLibrary\InteractsWithMedia; class Post extends Model implements HasMedia { use InteractsWithMedia; // Other model properties and methods public function registerMediaCollections(): void { $this->addMediaCollection('images') ->useFallbackUrl('/path/to/fallback/image.jpg') ->useFallbackPath('/path/to/fallback/image.jpg'); } public function registerMediaConversions(Media $media = null): void { $this->addMediaConversion('thumb') ->width(368) ->height(232) ->sharpen(10); $this->addMediaConversion('medium') ->width(800) ->height(600); } } |
Uploading Media
You can easily upload media files and associate them with a model:
1 2 3 4 5 |
$post = new Post(); $post->title = 'My First Post'; $post->save(); $post->addMedia($request->file('image'))->toMediaCollection('images'); |
Displaying Media
To display media in your views, you can retrieve the media items associated with the model:
1 2 3 |
@foreach ($post->getMedia('images') as $media) <img src="{{ $media->getUrl('thumb') }}" alt="{{ $media->name }}"> @endforeach |
Managing Conversions
You can define different conversions for your media files. For example, in the model’s registerMediaConversions
method, you can create multiple versions of an image with specific dimensions:
1 2 3 4 5 6 7 8 9 10 11 |
public function registerMediaConversions(Media $media = null): void { $this->addMediaConversion('thumb') ->width(368) ->height(232) ->sharpen(10); $this->addMediaConversion('large') ->width(1024) ->height(768); } |
Conclusion
Spatie Laravel Medialibrary Pro provides a robust solution for managing media files in your Laravel application. With features like image manipulation, file conversions, and easy association with Eloquent models, it simplifies the process of handling media.