Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tickteam-darylp's avatar

Custom Spatie/Media Library setup

I'm not sure if this is possible, or if someone has experience of setting this up, but can you use Spatie/MediaLibrary with a custom media solution?

We have our own system already where system users can manage their collections and resources that sit within them. Users will upload there (and from within content editing).

Rather than trying to re-invent the wheel with uploading, compressing, responsive images etc... I'm trying to work out if you can use parts of the Spatie/MediaLibrary, alongside a custom setup such as this.

We, and clients, really like the ability to be able to categorise their resources, rather than just having a single place that handles all media.

I guess, we'd end up using the Spatie Media table to store content, but use our own collections logic and using keys to attach media items to them too.

Has anyone tried to only partially use specific features from Spatie?

0 likes
2 replies
LaryAI's avatar
Level 58

To integrate Spatie's Media Library with a custom media solution, you can selectively use parts of the package while maintaining your existing system. Here's a general approach to achieve this:

  1. Install Spatie Media Library: First, ensure that you have the Spatie Media Library package installed in your Laravel project. You can do this via Composer:

    composer require spatie/laravel-medialibrary
    
  2. Set Up the Media Library: Follow the initial setup instructions provided by Spatie to publish the configuration file and run the necessary migrations:

    php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider"
    php artisan migrate
    
  3. Use Custom Collections: You can define custom collections in your models that implement the HasMedia interface. This allows you to categorize media files as needed.

    use Spatie\MediaLibrary\HasMedia;
    use Spatie\MediaLibrary\InteractsWithMedia;
    use Illuminate\Database\Eloquent\Model;
    
    class YourModel extends Model implements HasMedia
    {
        use InteractsWithMedia;
    
        public function registerMediaCollections(): void
        {
            $this->addMediaCollection('custom-collection');
        }
    }
    
  4. Integrate with Your System: Since you have an existing system for managing collections and resources, you can use Spatie's Media Library to handle the media files themselves (uploading, compressing, responsive images, etc.) while maintaining your own logic for collections.

    • Storing Media: Use Spatie's methods to store media files. You can attach media to your models using custom collections:

      $yourModel = YourModel::find($id);
      $yourModel->addMedia($pathToFile)->toMediaCollection('custom-collection');
      
    • Linking with Your Collections: You can use your existing logic to manage collections and link them to media items using foreign keys or pivot tables. This way, you can maintain your categorization system while leveraging Spatie's media handling capabilities.

  5. Partial Feature Usage: If you only want to use specific features of the Media Library, such as responsive images or conversions, you can configure these in your model's registerMediaConversions method:

    public function registerMediaConversions(Media $media = null): void
    {
        $this->addMediaConversion('thumb')
             ->width(368)
             ->height(232)
             ->sharpen(10);
    }
    
  6. Custom Logic: Implement any additional logic needed to integrate Spatie's features with your existing system. This might include custom queries, event listeners, or service classes to handle specific use cases.

By following these steps, you can effectively use Spatie's Media Library for its robust media handling features while maintaining your custom collection logic. This approach allows you to benefit from the package's capabilities without fully replacing your existing system.

tickteam-darylp's avatar

So I've managed to workout some of the queries we had and we can work around some limitations/incompatibilities between the two systems.

However, as we already have a custom directory setup for uploads (uploads/images/[date]/[file.name]) - we'd like to maintain that. Are there any Spatie/Media Library users who've achieved, not the same, but maybe a similar setup?

Also, what alternative packages have people used? I'm trying to do an overview of what is out there before deciding what route to take.

Ideally, if there is something out there that we can use - great. If not, then we can look to use a pre-made solution.

Please or to participate in this conversation.