Laravel-medialibrary: [Just curious] Use some kind of sync() method to update a collection?

Created on 13 Jul 2016  路  11Comments  路  Source: spatie/laravel-medialibrary

I'm just wondering how possible it would be to have some kind of sync() method (like we have for many-to-many relatinship) for the files?

I'm wondering because I'm thinking how I can work with, in a case of an article with images, multiple images being added/removed as article gets edited. I guess the easiest method would be to check for media by name in the array of getMedia(), but still, would it be hard to achieve a method that we could just... Send an array of filenames, it get checked against current collection, if new item, add it, if not present anymore, deletes it?

I'll sleep on this, but I'm curious to get your input on that!

Most helpful comment

I'll make that function a first class citizen one day and document it properly. 馃槃

All 11 comments

There's actually an undocumented method that can update media.

Honestly, I was hoping for some kind of surprise like this one! :) (That'll teach me not to scourge the source code :P) Thanks!

I'll make that function a first class citizen one day and document it properly. 馃槃

@freekmurze Hum, just to make sure, what do we feed updateMedia()'s mediaArray with? IDs of media, or filename path like with addMedia() ?

Here's an example of what the array looks like in our applications:

[
  [
    "id" => 247,
    "name" => "025e4912b552287a",
    "file_name" => "025e4912b552287a.jpg",
    "custom_properties" => [],
    "order_column" => 247,
  ],
  [
    "id" => 248,
    "name" => "837dfeec04a78a1c",
    "file_name" => "837dfeec04a78a1c.jpg",
    "custom_properties" => [],
    "order_column" => 248,
  ],
  [
    "id" => 249,
    "name" => "837dfeec04a78a1c",
    "file_name" => "837dfeec04a78a1c.jpg",
    "custom_properties" => [],
    "order_column" => 249,
  ],
]

Ok, so it's really just the result an array of the results for getMedia(). Thanks!

I'm receiving an error. Not sure exactly what I'm doing wrong; just trying to regenerate images...

$media = $post->getMedia()->toArray();
$post->updateMedia($media, 'mycollection');

Results in: Class 'Spatie\Medialibrary\Exceptions\MediaCannotBeUpdated' not found

Looks like there was a little bug in the package (case sensitivity issues with namespaces). I just pushed a fix, I'll tag a new version in a few minutes, after Travis has passed 馃憤

v4.8.1 has been tagged and should fix the issue!

Yep, that solved it, thanks!

I'm experiencing a different problem though; I have 3 images from when I saved the post. I now want to add 3 more images, each with a filter.

The workflow is like so:

  1. User creates a post and adds an image.
  2. Images are converted tosm, md, and lg, with original being preserved.
  3. user updates post and ticks checkbox to add a filter to image.

I need to be able to save the original images, PLUS add new conversions with the filter. I already have the code for creating the filter, so I'm really just trying to update an existing collection by adding 3 new images to it.

Directory after creating the post

  -- conversions
  ---- lg.jpg
  ---- md.jpg
  ---- sm.jpg
  -- original.jpg

Desired directory after user updates post and applies filter

  -- conversions
  ---- lg.jpg
  ---- md.jpg
  ---- sm.jpg
  ---- lg-with-filter.jpg
  ---- md-with-filter.jpg
  ---- sm-with-filter.jpg
  -- original.jpg

Hope this makes sense! :)

Edit: the original uploads have the collection_name "original" and the new images will have the collection name "filtered." This is where I'm experiencing the problem. When updating the post I receive the following error.

Media id 1 is not part of collection `filtered`

I'm guessing I need to add a new collection_name to the existing, but not sure how?

I think I may have it, but it just _feels_ really clunky. I had to build out the full path; I wasn't able to use $image->getPath() because I kept receiving the following error:

Call to undefined method App\SpatieUrlGenerator::getPath()

Here's what I'm working with so far...

$mediaItems = $post->getMedia();

foreach($mediaItems as $image) {
    $post->addMedia(storage_path('app/'.$image->getCustomProperty('hash').'/'.$image->file_name))
         ->usingFileName('with-filter-'.$image->file_name)
         ->withCustomProperties(['hash' => $image->getCustomProperty('hash'), 'visability' => 'filtered'])
         ->toCollection('filtered');
}

Is there a better way?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stayallive picture stayallive  路  4Comments

swash13 picture swash13  路  3Comments

jam1e picture jam1e  路  3Comments

brendt picture brendt  路  4Comments

drtheuns picture drtheuns  路  3Comments