Skip to content

Commit e6e0dac

Browse files
authored
Merge pull request #38 from mirco-dlab/master
Added soft deletes
2 parents 6735785 + f2d7042 commit e6e0dac

7 files changed

+65
-6
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Calling the `delete()` method on an attachment model instance will
124124
be disabled by setting the `behaviors.cascade_delete` to `false` in
125125
the configuration.
126126

127-
> Not that calling `delete()` on a `query()` like statement will not
127+
> Note that calling `delete()` on a `query()` like statement will not
128128
cascade to the filesystem because it will not call the `delete()`
129129
method of the `Attachment` model class.
130130

@@ -134,6 +134,15 @@ $attachmentByKey = $user->attachment('myKey');
134134
$attachmentByKey->delete(); // Will also delete the file on the storage by default
135135
```
136136

137+
### Soft Deletes
138+
The package also supports the Laravel's SoftDeletes trait (disabled by default).
139+
It can be enabled by setting the `behaviors.soft_delete` to `true` in
140+
the configuration.
141+
142+
> Note that `behaviors.cascade_delete` setting will then work only with
143+
the `forceDelete()` method of the `Attachment` model class.
144+
All the files will be kept while the record is soft-deleted
145+
137146

138147
## Hooking the file output
139148

config/attachments.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
*/
5757
'behaviors' => [
5858
'cascade_delete' => env('ATTACHMENTS_CASCADE_DELETE', true),
59+
'soft_delete' => env('ATTACHMENTS_SOFT_DELETE', false),
5960
'dropzone_check_csrf' => env('ATTACHMENTS_DROPZONE_CHECK_CSRF', true),
6061
],
6162

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class AlterAttachmentsTableAddDeletedAtColumn extends Migration
8+
{
9+
10+
/**
11+
* Run the migrations.
12+
*
13+
* @return void
14+
*/
15+
public function up()
16+
{
17+
if (Schema::hasTable('attachments')) {
18+
Schema::table('attachments', function (Blueprint $table) {
19+
$table->softDeletes();
20+
});
21+
}
22+
}
23+
24+
25+
/**
26+
* Reverse the migrations.
27+
*
28+
* @return void
29+
*/
30+
public function down()
31+
{
32+
if (Schema::hasTable('attachments')) {
33+
Schema::table('attachments', function (Blueprint $table) {
34+
$table->dropColumn('deleted_at');
35+
});
36+
}
37+
}
38+
}

src/Attachment.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Crypt;
88
use File as FileHelper;
99
use Illuminate\Database\Eloquent\Model;
10+
use Illuminate\Database\Eloquent\SoftDeletes;
1011
use Illuminate\Support\Arr;
1112
use Illuminate\Support\Str;
1213
use Storage;
@@ -40,6 +41,7 @@
4041
*/
4142
class Attachment extends Model implements AttachmentContract
4243
{
44+
use SoftDeletes;
4345

4446
protected $table = 'attachments';
4547

@@ -87,7 +89,7 @@ public static function attach($uuid, $model, $options = [])
8789
$attachment->fill($options);
8890

8991
if ($found = $model->attachments()->where('key', '=', $attachment->key)->first()) {
90-
$found->delete();
92+
$found->forceDelete();
9193
}
9294

9395
return $attachment->model()->associate($model)->save() ? $attachment : null;
@@ -211,8 +213,16 @@ protected static function boot()
211213
{
212214
parent::boot();
213215

216+
if (!config('attachments.behaviors.soft_delete', false)) {
217+
static::deleted(function ($attachment) {
218+
/** @var Attachment $attachment */
219+
220+
$attachment->forceDelete();
221+
});
222+
}
223+
214224
if (config('attachments.behaviors.cascade_delete')) {
215-
static::deleting(function ($attachment) {
225+
static::forceDeleting(function ($attachment) {
216226
/** @var Attachment $attachment */
217227

218228
$attachment->deleteFile();

src/Console/Commands/CleanupAttachments.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function handle()
4444
{
4545
if ($this->confirm(Lang::get('attachments::messages.console.cleanup_confirm'))) {
4646
$query = $this->model
47+
->withTrashed()
4748
->whereNull('model_type')
4849
->whereNull('model_id')
4950
->where('updated_at', '<=', Carbon::now()->addMinutes(-1 * $this->option('since')));
@@ -55,7 +56,7 @@ public function handle()
5556
/** @var Collection $attachments */
5657
$attachments->each(function ($attachment) use ($progress) {
5758
/** @var AttachmentContract $attachment */
58-
$attachment->delete();
59+
$attachment->forceDelete();
5960

6061
$progress->advance();
6162
});

src/Console/Commands/MigrateAttachments.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function handle()
7878
$this->error(Lang::get('attachments::messages.console.migrate_invalid_to'));
7979
}
8080

81-
$query = Attachment::query()
81+
$query = Attachment::withTrashed()
8282
->where('disk', '=', $this->argument('from'));
8383

8484
$this

src/Http/Controllers/DropzoneController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function delete($id, Request $request)
7474
return response(Lang::get('attachments::messages.errors.delete_denied'), 403);
7575
}
7676

77-
$file->delete();
77+
$file->forceDelete();
7878
}
7979

8080
return response('', 204);

0 commit comments

Comments
 (0)