You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since file deletion is often irreversible, this Laravel package queues file deletions within a database transaction, allowing for rollback in case of errors.
9
+
10
+
## The problem
11
+
12
+
Let's say you have a use case that resembles this:
13
+
14
+
```php
15
+
use App\Models\Image;
16
+
use Illuminate\Support\Facades\DB;
17
+
use Illuminate\Support\Facades\Storage;
18
+
19
+
DB::transaction(function () use ($fileIds) {
20
+
$images = Image::whereIn('id', $fileIds)->get();
21
+
22
+
foreach ($images as $image) {
23
+
if (Storage::exists($image->path)) {
24
+
Storage::delete($image->path);
25
+
}
26
+
27
+
// more logic ...
28
+
29
+
$image->delete();
30
+
}
31
+
});
32
+
```
33
+
34
+
If an error occurs while handling the second image, the database rows for both the first and second images will be rolled back by the transaction, but the actual file for the first image will be gone forever.
35
+
36
+
## The solution
37
+
38
+
```php
39
+
use App\Models\Image;
40
+
use Illuminate\Support\Facades\DB;
41
+
use Illuminate\Support\Facades\Storage;
42
+
use Medilies\RmQ\Facades\RmQ;
43
+
44
+
DB::transaction(function () use ($fileIds) {
45
+
$images = Image::whereIn('id', $fileIds)->get();
46
+
47
+
foreach ($images as $image) {
48
+
if (Storage::exists($image->path)) {
49
+
RmQ::stage($image->path);
50
+
}
51
+
52
+
// more logic ...
53
+
54
+
$image->delete();
55
+
}
56
+
});
57
+
```
58
+
59
+
This way, the file deletion is queued and the deletion can be fully rolled back.
9
60
10
61
## Installation
11
62
@@ -15,7 +66,7 @@ Install the package via composer:
0 commit comments