- 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.