File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change 23
23
use Illuminate \Database \Eloquent \Model ;
24
24
use Illuminate \Database \Eloquent \SoftDeletes ;
25
25
use InvalidArgumentException ;
26
+ use RuntimeException ;
26
27
27
28
class SoftDeleteDriver extends StandardDriver
28
29
{
@@ -81,11 +82,23 @@ public function persist(Model $model): bool
81
82
* @see https://github.com/cloudcreativity/laravel-json-api/issues/371
82
83
*/
83
84
if ($ this ->willSoftDelete ($ model )) {
85
+ assert (method_exists ($ model , 'getDeletedAtColumn ' ));
84
86
$ column = $ model ->getDeletedAtColumn ();
85
87
// save the original date so we can put it back later on.
86
88
$ deletedAt = $ model ->{$ column };
87
89
// delete the record so that deleting and deleted events get fired.
88
- $ model ->delete ();
90
+ $ response = $ model ->delete (); // capture the response
91
+
92
+ // if a listener prevented the delete from happening, we need to throw as we are in an invalid state.
93
+ // developers should prevent this scenario from happening either through authorization or validation.
94
+ if ($ response === false ) {
95
+ throw new RuntimeException (sprintf (
96
+ 'Failed to soft delete model - %s:%s ' ,
97
+ $ model ::class,
98
+ $ model ->getKey (),
99
+ ));
100
+ }
101
+
89
102
// apply the original date back before saving, so that we keep date provided by the client.
90
103
$ model ->{$ column } = $ deletedAt ;
91
104
}
Original file line number Diff line number Diff line change @@ -332,6 +332,23 @@ public function testItDoesNotSoftDeleteOnUpdate(): void
332
332
]));
333
333
}
334
334
335
+ public function testItDoesNotSoftDeleteOnUpdateIfListenerReturnsFalse (): void
336
+ {
337
+ $ post = Post::factory ()->create (['deleted_at ' => null ]);
338
+
339
+ $ data = ['deletedAt ' => now ()->toJSON (), 'title ' => 'Hello World! ' ];
340
+
341
+ Post::deleting (fn () => false );
342
+
343
+ $ this ->expectException (\RuntimeException::class);
344
+ $ this ->expectExceptionMessage ('Failed to soft delete model - App\Models\Post: ' . $ post ->getKey ());
345
+
346
+ $ this ->schema
347
+ ->repository ()
348
+ ->update ($ post )
349
+ ->store ($ data );
350
+ }
351
+
335
352
public function testItRestores (): void
336
353
{
337
354
$ restored = false ;
You can’t perform that action at this time.
0 commit comments