Allow spendings to be restored after being deleted
MODIFY
app/Http/Controllers/SpendingController.php
app/Http/Controllers/SpendingController.php
@@ -69,8 +69,28 @@ class SpendingController extends Controller {
public function destroy(Spending $spending) {
$this->authorize('delete', $spending);
+ $restorableSpending = $spending->id;
+
$spending->delete();
+ return redirect()
+ ->route('spendings.index')
+ ->with([
+ 'restorableSpending' => $restorableSpending
+ ]);
+ }
+
+ public function restore($id) {
+ $spending = Spending::withTrashed()->find($id);
+
+ if (!$spending) {
+ // 404
+ }
+
+ $this->authorize('restore', $spending);
+
+ $spending->restore();
+
return redirect()->route('spendings.index');
}
}

MODIFY
app/Policies/SpendingPolicy.php
app/Policies/SpendingPolicy.php
@@ -13,4 +13,8 @@ class SpendingPolicy {
public function delete(User $user, Spending $spending) {
return $user->spaces->contains($spending->space_id);
}
+
+ public function restore(User $user, Spending $spending) {
+ return $user->spaces->contains($spending->space_id);
+ }
}

MODIFY
resources/views/spendings/index.blade.php
resources/views/spendings/index.blade.php
</div>
@endif
</div>
+ @if (session('restorableSpending'))
+ <div class="mt-3">You've successfully deleted that spending</div>
+ <form method="POST" action="/spendings/{{ session('restorableSpending') }}/restore" class="mt-05">
+ {{ csrf_field() }}
+ <button class="button link">You can still recover it</button>
+ </form>
+ @endif
@if (count($spendingsByMonth))
@foreach ($spendingsByMonth as $index => $spendings)
@if (count($spendings))

MODIFY
routes/web.php
routes/web.php
@@ -31,6 +31,7 @@ Route::group(['middleware' => ['auth']], function () {
Route::get('/spendings/create', 'SpendingController@create')->name('create');
Route::post('/spendings', 'SpendingController@store');
Route::delete('/spendings/{spending}', 'SpendingController@destroy');
+ Route::post('/spendings/{id}/restore', 'SpendingController@restore');
});
Route::resource('/recurrings', 'RecurringController')->only([

Allow spendings to be restored after being deleted