Display button for editing transactions on its overview page
MODIFY
app/Http/Controllers/EarningController.php
app/Http/Controllers/EarningController.php
@@ -65,7 +65,7 @@ class EarningController extends Controller {
'amount' => $request->input('amount')
])->save();
- return redirect()->route('earnings.index');
+ return redirect()->route('transactions.index');
}
public function destroy(Earning $earning) {

MODIFY
app/Http/Controllers/SpendingController.php
app/Http/Controllers/SpendingController.php
@@ -66,6 +66,31 @@ class SpendingController extends Controller {
return redirect()->route('dashboard');
}
+ public function edit(Spending $spending) {
+ $this->authorize('edit', $spending);
+
+ return view('spendings.edit', compact('spending'));
+ }
+
+ public function update(Request $request, Spending $spending) {
+ $this->authorize('update', $spending);
+
+ // TODO MOVE TO CENTRAL PLACE FOR REUSABILITY
+ $request->validate([
+ 'date' => 'required|date|date_format:Y-m-d',
+ 'description' => 'required|max:255',
+ 'amount' => 'required|regex:/^\d*(\.\d{2})?$/'
+ ]);
+
+ $spending->fill([
+ 'happened_on' => $request->input('date'),
+ 'description' => $request->input('description'),
+ 'amount' => (int) ($request->input('amount') * 100)
+ ])->save();
+
+ return redirect()->route('transactions.index');
+ }
+
public function destroy(Spending $spending) {
$this->authorize('delete', $spending);

MODIFY
app/Policies/SpendingPolicy.php
app/Policies/SpendingPolicy.php
@@ -10,6 +10,14 @@ class SpendingPolicy {
return $user->spaces->contains($spending->space_id);
}
+ public function edit(User $user, Spending $spending) {
+ return $user->spaces->contains($spending->space_id);
+ }
+
+ public function update(User $user, Spending $spending) {
+ return $user->spaces->contains($spending->space_id);
+ }
+
public function delete(User $user, Spending $spending) {
return $user->spaces->contains($spending->space_id);
}

ADD
resources/views/spendings/edit.blade.php
+@extends('layout')
+
+@section('title', __('actions.edit') . ' ' . __('models.spending'))
+
+@section('body')
+ <div class="wrapper my-3">
+ <h2>{{ __('actions.edit') }} {{ __('models.spending') }}</h2>
+ <div class="box mt-3">
+ <form method="POST" action="/spendings/{{ $spending->id }}" autocomplete="off">
+ {{ method_field('PATCH') }}
+ {{ csrf_field() }}
+ <div class="box__section">
+ <div class="input input--small">
+ <label>Date</label>
+ <DatePicker start-date="{{ $spending->happened_on }}"></DatePicker>
+ @include('partials.validation_error', ['payload' => 'date'])
+ </div>
+ <div class="input input--small">
+ <label>Description</label>
+ <input type="text" name="description" value="{{ $spending->description }}" />
+ @include('partials.validation_error', ['payload' => 'description'])
+ </div>
+ <div class="input input--small mb-0">
+ <label>Amount</label>
+ <input type="text" name="amount" value="{{ $spending->formatted_amount }}" />
+ @include('partials.validation_error', ['payload' => 'amount'])
+ </div>
+ </div>
+ <div class="box__section box__section--highlight row row--right">
+ <div class="row__column row__column--compact row__column--middle">
+ <a href="/spendings">{{ __('actions.cancel') }}</a>
+ </div>
+ <div class="row__column row__column--compact ml-2">
+ <button class="button">@lang('actions.save')</button>
+ </div>
+ </div>
+ </form>
+ </div>
+ </div>
+@endsection

MODIFY
resources/views/transactions/index.blade.php
resources/views/transactions/index.blade.php
<div class="box__section row">
<div class="row__column row__column--middle row row--middle">
<div>{{ $transaction->description }}</div>
+ <a href="/{{ get_class($transaction) === 'App\Models\Earning' ? 'earnings' : 'spendings' }}/{{ $transaction->id }}/edit">
+ <i class="fas fa-pencil fa-xs c-light ml-1"></i>
+ </a>
<form action="/{{ get_class($transaction) === 'App\Models\Earning' ? 'earnings' : 'spendings' }}/{{ $transaction->id }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}

MODIFY
routes/web.php
routes/web.php
@@ -37,6 +37,8 @@ Route::group(['middleware' => ['auth']], function () {
Route::get('/spendings', 'SpendingController@index')->name('index');
Route::get('/spendings/create', 'SpendingController@create')->name('create');
Route::post('/spendings', 'SpendingController@store');
+ Route::get('/spendings/{spending}/edit', 'SpendingController@edit')->name('edit');
+ Route::patch('/spendings/{spending}', 'SpendingController@update');
Route::delete('/spendings/{spending}', 'SpendingController@destroy');
Route::post('/spendings/{id}/restore', 'SpendingController@restore');
});

Display button for editing transactions on its overview page