@@ -275,7 +275,36 @@ public function getParentConversion(string $name): ?MediaConversion
275
275
*/
276
276
public function getChildrenConversions (string $ name ): EloquentCollection
277
277
{
278
- return $ this ->conversions ->filter (fn ($ conversion ) => str_starts_with ($ conversion ->conversion_name , "{$ name }. " ));
278
+ return $ this
279
+ ->conversions
280
+ ->filter (fn ($ conversion ) => str_starts_with ($ conversion ->conversion_name , "{$ name }. " ));
281
+ }
282
+
283
+ public function replaceConversion (
284
+ MediaConversion $ conversion ,
285
+ ): MediaConversion {
286
+
287
+ $ existingConversion = $ this ->getConversion ($ conversion ->conversion_name );
288
+
289
+ if (
290
+ $ conversion ->exists ||
291
+ $ conversion ->is ($ existingConversion )
292
+ ) {
293
+ return $ conversion ;
294
+ }
295
+
296
+ $ this ->conversions ()->save ($ conversion );
297
+ $ this ->conversions ->push ($ conversion );
298
+
299
+ if ($ existingConversion ) {
300
+ $ existingConversion ->delete ();
301
+ $ this ->setRelation (
302
+ 'conversions ' ,
303
+ $ this ->conversions ->except ([$ existingConversion ->id ])
304
+ );
305
+ }
306
+
307
+ return $ conversion ;
279
308
}
280
309
281
310
/**
@@ -320,7 +349,7 @@ public function addConversion(
320
349
321
350
if ($ existingConversion ) {
322
351
$ existingConversion ->deleteFile ();
323
- $ this ->deleteChildrenConversion ($ conversionName );
352
+ $ this ->deleteChildrenConversions ($ conversionName );
324
353
} else {
325
354
$ this ->conversions ->push ($ conversion );
326
355
}
@@ -396,12 +425,10 @@ public function deleteConversion(string $conversionName): static
396
425
return $ this ;
397
426
}
398
427
399
- public function deleteChildrenConversion (string $ conversionName ): static
428
+ public function deleteChildrenConversions (string $ conversionName ): static
400
429
{
401
- $ deleted = $ this ->conversions
402
- ->filter (function ($ conversion ) use ($ conversionName ) {
403
- return str ($ conversion ->conversion_name )->startsWith ("{$ conversionName }. " );
404
- })
430
+ $ deleted = $ this
431
+ ->getChildrenConversions ($ conversionName )
405
432
->each (fn ($ conversion ) => $ conversion ->delete ());
406
433
407
434
$ this ->setRelation (
0 commit comments