Laravel-medialibrary: Central media library referenced by other models

Created on 17 Aug 2018  Â·  2Comments  Â·  Source: spatie/laravel-medialibrary

The medialibrary package seems to work very well when it's a simple model to media relationship. But is it possible to have a central media library (similar to WordPress) which any model can reference?

For example:

  1. Go to /media and upload multiple files
  2. Go to a model e.g. /task and select an image from the library

I understand that once the image is in the library it is given an ID so I can reference that from the other models, but the bit I'm unsure of is how to handle the upload and whether to create a 'MediaLibrary' model that the images attach to.

Has anyone done this? Can anyone share some tips?

Most helpful comment

@riseoflex88 I think you can do what you want by attaching your medias to the user model within a “uploads” collection. Then select the media and copy/move it to your other model

All 2 comments

Reading the article here: https://murze.be/comparing-laravel-mediable-with-laravel-medialibrary I'm realising that Spatie's medialibrary really is designed to explicitly not support what I'm hoping to do.

I guess my question is, can this be done with a reasonable workaround (a not-too-bodgy bodge)?

@riseoflex88 I think you can do what you want by attaching your medias to the user model within a “uploads” collection. Then select the media and copy/move it to your other model

Was this page helpful?
0 / 5 - 0 ratings