Laravel-medialibrary: Add directly to media table

Created on 25 Apr 2018  Â·  10Comments  Â·  Source: spatie/laravel-medialibrary

All examples I have seen use the $model->addMedia() way to upload the media...
I was wondering if there is anyway to add directly into the media table, return the id and use it somewhere else?

I am wondering this because I have to upload the media before creating the entity so I can't just do
$model->addMedia()...

Thanks

Most helpful comment

I'm working on a built in mechanism to help this. It's a mechanism where you can add media to temporary upload model and move that to a real model when the form is submitted.

Not documented yet: work being done here:
https://github.com/spatie/laravel-medialibrary/tree/temporary-uploads

All 10 comments

You can actually attach media to unsaved entities

$model = new Model();

$model->addMedia(...);

$model->save();

$id = $model->media->first()->id;

Oh that's cool, thanks @freekmurze ... Is that mentioned on the documentation? I searched but couldn't find it.

@freekmurze So i still can't get media id before model save?
Sometime, there is a dropzone case, page need media id,before page submit, and dropzone don't kown which the model is

@tim0991 That`s just what I needed... So I ended-up having another model just to save the media and returning its ID so I can reuse on the other entity (the one not saved yet)... My case was also for a dropzone file
``

Hi @fernandocoronatomf, @freekmurze

Can you please explain little bit more.I'm trying to integrate my laravel app with vuejs and dropzone .I already try many way to upload and manage media.I still didn't found proper way. my problem is also to upload media before saving entity like @fernandocoronatomf said.Please give me any refrence links if u have.

Thanks!

Hi @nyeyint...
What I did basically was creating a new table, let's call it EntityMedia.
I upload the file there using dropzone, and it creates a new entity with its own id...
Then I use this ID on the main entity...

Hi @fernandocoronatomf ...

You mean save media to media talbe with EntityMedia model name and return url or id like this.

image.

that work but.how about duplication in media table ?

So, those building APIs still can't upload files in background while user keep filling a form... :/

This functionality would be useful for implementing a markdown editor. I have had to make a model that only persists an id to addMedia to.

@freekmurze maybe an idea for next release?

I'm working on a built in mechanism to help this. It's a mechanism where you can add media to temporary upload model and move that to a real model when the form is submitted.

Not documented yet: work being done here:
https://github.com/spatie/laravel-medialibrary/tree/temporary-uploads

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Krato picture Krato  Â·  4Comments

ideadx picture ideadx  Â·  4Comments

snapey picture snapey  Â·  3Comments

brendt picture brendt  Â·  4Comments

Nks picture Nks  Â·  3Comments