Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Laravel Media


Laravel Media


Laravel Media is a package designed to attach files to Eloquent models, providing image manipulation capabilities. Here are the key concepts and usage details:

Installation

1. Install the Package:

bash
   composer require optix/media
   

2. Publish Assets:

bash
   php artisan vendor:publish --provider="Optix\Media\MediaServiceProvider"
   

Key Concepts

1. Media:
- Media can be any type of file.
- Media is uploaded as its own entity and does not belong to another model initially.
- Media items are bound to "groups", making it easy to associate multiple types of media to a model.

2. Conversions:
- Conversions are registered globally and can be reused across the application.
- Conversions can be specified when a media item is associated to a model.

Usage

1. Upload Media:
- Use the `Optix\Media\MediaUploader` class to handle file uploads.
- Customise certain properties of the file before uploading:

php
     $file = $request->file('file');
     $media = MediaUploader::fromFile($file)
         ->useFileName('custom-file-name.jpeg')
         ->useName('Custom media name')
         ->upload();
     

2. Associate Media with a Model:
- Include the `Optix\Media\HasMedia` trait in your model.
- Attach media to the model:

php
     $post = Post::first();
     $post->attachMedia($media);
     $post->attachMedia($media, 'custom-group');
     

3. Disassociate Media:
- Detach media from a model:

php
     $post->detachMedia();
     $post->detachMedia($media);
     $post->clearMediaGroup('your-group');
     

Example Usage

Here is an example of how to use the package:

php
// Define the model
class Post extends Model
{
    use HasMedia;
}

// Upload media
$file = $request->file('file');
$media = MediaUploader::fromFile($file)->upload();

// Attach media to the model
$post = Post::first();
$post->attachMedia($media);

// Detach media
$post->detachMedia();

Laravel Media Library by Spatie

The Laravel Media Library by Spatie is another package that allows associating files with Eloquent models. Here are the key points:

1. Installation:

bash
   composer require spatie/laravel-medialibrary
   

2. Basic Usage:
- Add media to a model:

php
     $newsItem = News::find(1);
     $newsItem->addMedia($pathToFile)->toMediaCollection('images');
     

3. Handling Large Files:
- Store large files on another filesystem:

php
     $newsItem->addMedia($smallFile)->toMediaCollection('downloads', 'local');
     $newsItem->addMedia($bigFile)->toMediaCollection('downloads', 's3');
     

4. Image Manipulations:
- Generate derived images like thumbnails:

php
     $yourModel->getMedia('images')->first()->getUrl('thumb');
     

Documentation

For detailed documentation and examples, refer to the official documentation for both packages:

- Optix Laravel Media: https://github.com/optixsolutions/laravel-media
- Spatie Laravel Media Library: https://spatie.be/docs/laravel-medialibrary/v11/introduction

These packages provide robust solutions for managing media files in Laravel applications, offering flexibility and customization options for various use cases[1][3][4][5].

Citations:
[1] https://github.com/optixsolutions/laravel-media
[2] https://www.youtube.com/watch?v=FeIYurxiggU
[3] https://spatie.be/docs/laravel-medialibrary/v11/introduction
[4] https://github.com/spatie/laravel-medialibrary
[5] https://spatie.be/docs/laravel-medialibrary/v11/working-with-media-collections/defining-media-collections