Add delete function to SpendingPolicy
MODIFY
app/Http/Controllers/SpendingsController.php
app/Http/Controllers/SpendingsController.php
@@ -43,9 +43,7 @@ class SpendingsController extends Controller {
return view('spendings.show', compact('currency', 'spending'));
}
- public function destroy($id) {
- $spending = Spending::find($id);
-
+ public function destroy(Spending $spending) {
$year = date('Y', strtotime($spending->date));
$month = date('n', strtotime($spending->date));

MODIFY
app/Policies/SpendingPolicy.php
app/Policies/SpendingPolicy.php
@@ -9,4 +9,8 @@ class SpendingPolicy {
public function view(User $user, Spending $spending) {
return $user->id === $spending->user_id;
}
+
+ public function delete(User $user, Spending $spending) {
+ return $user->id === $spending->user_id;
+ }
}

MODIFY
routes/web.php
routes/web.php
@@ -21,7 +21,7 @@ Route::group(['middleware' => ['auth']], function () {
Route::get('/spendings/create', 'SpendingsController@create')->name('spendings.create');
Route::post('/spendings', 'SpendingsController@store');
Route::get('/spendings/{spending}', 'SpendingsController@show')->name('spendings.show')->middleware('can:view,spending');
- Route::delete('/spendings/{id}', 'SpendingsController@destroy');
+ Route::delete('/spendings/{spending}', 'SpendingsController@destroy')->middleware('can:delete,spending');
Route::get('/budgets/create', 'BudgetsController@create')->name('budgets.create');
Route::post('/budgets', 'BudgetsController@store');

Add delete function to SpendingPolicy