Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Laravel Medialibrary

Laravel Medialibrary Laravel Package

spatie/laravel-medialibrary

Attach and manage files on Eloquent models with an easy API. Handle uploads, store media on any Laravel filesystem (local, S3, etc.), organize collections, and generate image/PDF conversions and manipulations with built-in support for responsive images.

View on GitHub
Deep Wiki
Context7

Associate files with Eloquent models

Frequently asked questions about Laravel Medialibrary
How do I attach a file to an Eloquent model using laravel-medialibrary?
Use the `addMedia()` method followed by `toMediaCollection()`. For example, `$post->addMedia($filePath)->toMediaCollection('images')` attaches a file to a 'images' collection on the model. For uploads, use `addMediaFromRequest()` with the request file.
Can I store different media collections on different disks (e.g., S3 for large files, local for thumbnails)?
Yes, specify the disk when attaching media. For example, `$post->addMedia($file)->toMediaCollection('downloads', 's3')` stores the file on the 's3' disk. This leverages Laravel’s filesystem drivers for flexibility.
Does laravel-medialibrary support image resizing or PDF manipulation?
Yes, it integrates with `spatie/image` (optional dependency) to generate thumbnails, resize images, or manipulate PDFs. Use methods like `generateThumbnails()` or `generatePdfManipulations()` after attaching media.
What Laravel versions does spatie/laravel-medialibrary support?
The package supports Laravel 9.x, 10.x, and 11.x. Check the [GitHub releases](https://github.com/spatie/laravel-medialibrary/releases) for version-specific compatibility notes. Always ensure your Laravel version matches the package’s requirements.
How do I handle file uploads from a form in a Laravel controller?
Use `addMediaFromRequest()` to attach uploaded files directly. For example, `$post->addMediaFromRequest('image')->toMediaCollection('thumbnails')` processes the file from the request and stores it in the 'thumbnails' collection.
Will media be deleted when the associated Eloquent model is soft-deleted?
No, by default. Use `deletePreservingMedia()` to soft-delete the model while keeping its media. To delete media during soft deletes, override the `delete()` method or use `deleteWithMedia()`. Always test this behavior in staging.
Can I fetch or migrate existing files into laravel-medialibrary?
Yes, use `addMediaFromUrl()` to fetch remote files or manually insert records into the `media` table for existing files. The package provides helper methods and migrations to streamline this process.
How do I restrict media access to authenticated users only?
Use Laravel’s middleware or policies to gate access to media URLs. For S3, generate temporary URLs with `Storage::disk('s3')->temporaryUrl()`. Combine this with authentication checks in your routes or controllers.
Are there performance concerns with large-scale media libraries?
Yes, image manipulations and database queries can impact performance. Offload processing to queues (e.g., Laravel Horizon) and optimize queries with `withMedia()` or eager loading. For high traffic, consider caching or CDN strategies.
What alternatives to laravel-medialibrary exist, and how do they compare?
Alternatives include `laravel-stapler` (simpler but less feature-rich) and `mediable` (older, less maintained). laravel-medialibrary stands out for its fluent API, filesystem flexibility, and built-in image/PDF manipulation. Evaluate based on your needs for collections, storage backends, and scalability.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai