public function monthlyExpenses(Request $request, $month = null) { $months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; $otherMonth = isset($month) && ($month > 0 && $month < 13); $currentMonthTrackedExpenses = TrackedExpenses::query(); if ($otherMonth) { //Get a Specific Months Data $monthStart = Carbon::create(date('Y'), $month, 1, 0, 0, 0); $monthEnd = Carbon::create(date('Y'), $month, 1, 0, 0, 0)->endOfMonth(); $currentMonthTrackedExpenses = $currentMonthTrackedExpenses->where('created_at', '>=', $monthStart)->where('created_at', '<=', $monthEnd); if ($month == 1) { $monthStart = clone $monthStart; $monthStart->previous()->startOfMonth(); $monthEnd = clone $monthEnd; $monthEnd = $monthEnd->firstOfMonth()->previous()->endOfMonth(); } else { $monthStart = $monthStart->previous()->startOfMonth(); $monthEnd = $monthEnd->previous()->lastOfMonth(); } $lastMonthData = TrackedExpenses::where('created_at', '>=', $monthStart)->where('created_at', '<=', $monthEnd)->orderBy('id', 'asc')->get()->toArray(); } else { //Get current Months Data $currentMonthTrackedExpenses = $currentMonthTrackedExpenses->where('created_at', '>=', Carbon::now()->startOfMonth())->where('created_at', '<=', Carbon::now()->endOfMonth()); $lastMonthData = TrackedExpenses::where('created_at', '>=', Carbon::now()->startOfMonth()->previous()->firstOfMonth())->where('created_at', '<=', Carbon::now()->startOfMonth()->previous()->lastOfMonth())->get()->toArray(); } //$lastMonthData = $lastMonthData->orderBy('id','desc')->get()->toArray(); $currentMonthTrackedExpenses = $currentMonthTrackedExpenses->orderBy('id', 'desc')->get(); $expenses = Expense::where('active', 1)->get(); $current = 0; foreach ($expenses as $expense) { $e[$current]['expense'] = $expense; $e[$current]['monthlypayed'] = $this->findExpense($expense->id, $currentMonthTrackedExpenses); $e[$current]['lastMonthCost'] = $this->findExpenseLastMonth($expense->id, $lastMonthData); $current++; } return ['month' => $otherMonth ? $months[$month - 1] : Date('F'), 'payments' => $e, 'year' => date('Y')]; }
<?php /* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | Here is where you can register all of the routes for an application. | It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the controller to call when that URI is requested. | */ // //Event::listen('illuminate.query', function($query) //{ // var_dump($query); //}); Route::get('/', function () { return view('home'); }); Route::resource('expense', 'ExpensesController'); Route::resource('trackedexpense', 'TrackedExpensesController'); Route::group(['prefix' => 'pages'], function () { Route::get('/monthlyexpenses', 'PagesController@monthlyExpenses'); Route::get('/monthlyexpenses/{month}', 'PagesController@monthlyExpenses'); Route::get('/totalmonthlyexpenses', 'PagesController@totalMonthlyExpenses'); Route::get('/totalmonthlyexpenses/{expenseId}', 'PagesController@totalMonthlyExpenses'); }); Route::get('test', function () { return \App\TrackedExpenses::with('expense')->get(); });
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $te = TrackedExpenses::findOrFail($id); $te->fill($request->all()); $te->save(); }