Allow earnings to be restored from soft-deleted state
MODIFY
app/Http/Controllers/EarningController.php
app/Http/Controllers/EarningController.php
@@ -71,8 +71,28 @@ class EarningController extends Controller {
public function destroy(Earning $earning) {
$this->authorize('delete', $earning);
+ $restorableEarning = $earning->id;
+
$earning->delete();
+ return redirect()
+ ->route('earnings.index')
+ ->with([
+ 'restorableEarning' => $restorableEarning
+ ]);
+ }
+
+ public function restore($id) {
+ $earning = Earning::withTrashed()->find($id);
+
+ if (!$earning) {
+ // 404
+ }
+
+ $this->authorize('restore', $earning);
+
+ $earning->restore();
+
return redirect()->route('earnings.index');
}
}

MODIFY
app/Policies/EarningPolicy.php
app/Policies/EarningPolicy.php
@@ -21,4 +21,8 @@ class EarningPolicy {
public function delete(User $user, Earning $earning) {
return $user->spaces->contains($earning->space_id);
}
+
+ public function restore(User $user, Earning $earning) {
+ return $user->spaces->contains($earning->space_id);
+ }
}

MODIFY
resources/views/earnings/index.blade.php
resources/views/earnings/index.blade.php
<a href="/earnings/create" class="button">{{ __('actions.create') }} {{ __('models.earning') }}</a>
</div>
</div>
+ @if (session('restorableEarning'))
+ <div class="mt-3">You've successfully deleted that earning</div>
+ <form method="POST" action="/earnings/{{ session('restorableEarning') }}/restore" class="mt-05">
+ {{ csrf_field() }}
+ <button class="button link">You can still recover it</button>
+ </form>
+ @endif
<div class="box mt-3">
@if (count($earnings))
@foreach ($earnings as $earning)

MODIFY
routes/web.php
routes/web.php
@@ -25,6 +25,10 @@ Route::group(['middleware' => ['auth']], function () {
'destroy'
]);
+ Route::name('earnings.')->group(function () {
+ Route::post('/earnings/{id}/restore', 'EarningController@restore');
+ });
+
Route::resource('/spendings', 'SpendingController')->only([
'index',
'create',

Allow earnings to be restored from soft-deleted state