|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers; |
| 4 | + |
| 5 | +use App\Models\Category; |
| 6 | +use App\Models\Transaction; |
| 7 | +use Illuminate\Http\Request; |
| 8 | + |
| 9 | +class TransactionController extends Controller |
| 10 | +{ |
| 11 | + public function index() |
| 12 | + { |
| 13 | + $title = "Transaction Page"; |
| 14 | + $transactions = Transaction::with("category")->get(); |
| 15 | + |
| 16 | + return view("/transaction/index", compact("title", "transactions")); |
| 17 | + } |
| 18 | + |
| 19 | + |
| 20 | + public function addOutcomeGet() |
| 21 | + { |
| 22 | + $title = "Add Outcome"; |
| 23 | + $categories = Category::where("id", "!=", 1)->get(); |
| 24 | + |
| 25 | + return view("/transaction/add_outcome", compact("title", "categories")); |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | + public function addOutcomePost(Request $request) |
| 30 | + { |
| 31 | + $validatedData = $request->validate( |
| 32 | + [ |
| 33 | + "category_id" => "required|numeric|gt:0", |
| 34 | + "outcome" => "required|numeric|gt:0", |
| 35 | + "description" => "required" |
| 36 | + ], |
| 37 | + [ |
| 38 | + "category_id.gt" => "Please select the category" |
| 39 | + ] |
| 40 | + ); |
| 41 | + |
| 42 | + Transaction::create($validatedData); |
| 43 | + |
| 44 | + $message = "Transaction created successfully!"; |
| 45 | + |
| 46 | + myFlasherBuilder(message: $message, success: true); |
| 47 | + |
| 48 | + return redirect("/transaction"); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + public function editOutcomeGet(Transaction $transaction) |
| 53 | + { |
| 54 | + $title = "Edit Outcome"; |
| 55 | + $transaction->load("category"); |
| 56 | + $categories = Category::where("id", "!=", 1)->get(); |
| 57 | + |
| 58 | + return view("/transaction/edit_outcome", compact("title", "transaction", "categories")); |
| 59 | + } |
| 60 | + |
| 61 | + public function editOutcomePost(Request $request, Transaction $transaction) |
| 62 | + { |
| 63 | + $validatedData = $request->validate( |
| 64 | + [ |
| 65 | + "category_id" => "required|numeric|gt:0", |
| 66 | + "outcome" => "required|numeric|gt:0", |
| 67 | + "description" => "required" |
| 68 | + ], |
| 69 | + [ |
| 70 | + "category_id.gt" => "Please select the category" |
| 71 | + ] |
| 72 | + ); |
| 73 | + |
| 74 | + $transaction->fill($validatedData); |
| 75 | + |
| 76 | + if ($transaction->isDirty()) { |
| 77 | + $transaction->save(); |
| 78 | + |
| 79 | + |
| 80 | + $message = "Transaction updated successfully!"; |
| 81 | + |
| 82 | + myFlasherBuilder(message: $message, success: true); |
| 83 | + |
| 84 | + return redirect("/transaction"); |
| 85 | + } else { |
| 86 | + $message = "Action failed, no changes detected!"; |
| 87 | + |
| 88 | + myFlasherBuilder(message: $message, failed: true); |
| 89 | + |
| 90 | + return back(); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments