Top Related Projects
Easy creation of slugs for your Eloquent models in Laravel
Media gallery with CKEditor, TinyMCE and Summernote support. Built on Laravel file system.
Quick Overview
Laravel Media Library is a powerful package for managing media files in Laravel applications. It provides an easy-to-use interface for associating files with Eloquent models, handling conversions, and generating URLs for media items.
Pros
- Seamless integration with Laravel and Eloquent models
- Supports various file storage systems (local, S3, etc.)
- Automatic image conversions and responsive images
- Extensive documentation and active community support
Cons
- Can be resource-intensive for large media collections
- Learning curve for advanced features
- Limited built-in support for video processing
- Requires additional configuration for certain storage drivers
Code Examples
- Attaching media to a model:
$model->addMedia($pathToFile)->toMediaCollection('images');
- Retrieving media URLs:
$mediaUrl = $model->getFirstMediaUrl('images');
- Generating responsive images:
$model->addMedia($pathToFile)
->withResponsiveImages()
->toMediaCollection('images');
- Applying conversions:
$model->addMedia($pathToFile)
->withManipulations(['thumb' => ['width' => 100, 'height' => 100]])
->toMediaCollection('images');
Getting Started
- Install the package via Composer:
composer require spatie/laravel-medialibrary
- Publish the configuration file:
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="config"
- Run migrations:
php artisan migrate
- Add the
HasMedia
interface andInteractsWithMedia
trait to your model:
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class YourModel extends Model implements HasMedia
{
use InteractsWithMedia;
}
- Start using the Media Library in your application!
Competitor Comparisons
Easy creation of slugs for your Eloquent models in Laravel
Pros of eloquent-sluggable
- Focused specifically on slug generation for Eloquent models
- Offers more customization options for slug creation
- Lighter weight and easier to integrate for simple slug needs
Cons of eloquent-sluggable
- Limited to slug functionality, unlike laravel-medialibrary's broader media handling
- Lacks advanced features like file conversions and responsive images
- May require additional packages for more complex media management tasks
Code Comparison
eloquent-sluggable:
use Cviebrock\EloquentSluggable\Sluggable;
class Post extends Model
{
use Sluggable;
public function sluggable(): array
{
return [
'slug' => [
'source' => 'title'
]
];
}
}
laravel-medialibrary:
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class Post extends Model implements HasMedia
{
use InteractsWithMedia;
public function registerMediaCollections(): void
{
$this->addMediaCollection('images');
}
}
Both packages offer easy integration with Laravel Eloquent models, but they serve different purposes. eloquent-sluggable is focused on generating and managing slugs, while laravel-medialibrary provides comprehensive media handling capabilities. Choose based on your specific project requirements.
Media gallery with CKEditor, TinyMCE and Summernote support. Built on Laravel file system.
Pros of laravel-filemanager
- Provides a user-friendly file manager interface with drag-and-drop functionality
- Supports image editing features like cropping and resizing
- Offers multi-language support out of the box
Cons of laravel-filemanager
- Less flexible for programmatic file handling compared to laravel-medialibrary
- May have a steeper learning curve for developers due to its UI-centric approach
- Limited options for custom file conversions and manipulations
Code Comparison
laravel-filemanager:
$lfm = app()->make('lfm');
$lfm->getFiles('/path/to/folder');
laravel-medialibrary:
$model->addMedia($pathToFile)
->toMediaCollection('images');
$model->getMedia('images');
The laravel-filemanager code focuses on retrieving files from a specific folder, while laravel-medialibrary demonstrates adding media to a model and retrieving it from a collection. This highlights the different approaches: laravel-filemanager is more about managing files in a traditional folder structure, whereas laravel-medialibrary is designed for associating media with Eloquent models.
laravel-filemanager is better suited for projects requiring a ready-to-use file management interface, while laravel-medialibrary offers more flexibility for developers who need programmatic control over media handling and want to integrate it tightly with their application's data models.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Associate files with Eloquent models
This package can associate all sorts of files with Eloquent models. It provides a simple API to work with. To learn all about it, head over to the extensive documentation.
Here are a few short examples of what you can do:
$newsItem = News::find(1);
$newsItem->addMedia($pathToFile)->toMediaCollection('images');
It can handle your uploads directly:
$newsItem->addMedia($request->file('image'))->toMediaCollection('images');
Want to store some large files on another filesystem? No problem:
$newsItem->addMedia($smallFile)->toMediaCollection('downloads', 'local');
$newsItem->addMedia($bigFile)->toMediaCollection('downloads', 's3');
The storage of the files is handled by Laravel's Filesystem, so you can use any filesystem you like. Additionally, the package can create image manipulations on images and pdfs that have been added in the media library.
Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Documentation
You'll find the documentation on https://spatie.be/docs/laravel-medialibrary.
Find yourself stuck using the package? Found a bug? Do you have general questions or suggestions for improving the media library? Feel free to create an issue on GitHub, we'll try to address it as soon as possible.
If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.
Testing
You can run the tests with:
./vendor/bin/pest
You can run the Github actions locally with act. You have to use a custom image for the ubuntu-latest platform to get PHP up and running properly. To run the tests locally, run:
act -P ubuntu-latest=shivammathur/node:latest
To run a specific workflow, for example run-tests.yml
run:
act -P ubuntu-latest=shivammathur/node:latest -j run-tests
Upgrading
Please see UPGRADING for details.
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email security@spatie.be instead of using the issue tracker.
Credits
A big thank you to Nicolas Beauvais for helping out with the issues on this repo.
And a special thanks to Caneco for the logo â¨
Alternatives
License
The MIT License (MIT). Please see License File for more information.
Top Related Projects
Easy creation of slugs for your Eloquent models in Laravel
Media gallery with CKEditor, TinyMCE and Summernote support. Built on Laravel file system.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot