CodeIgniter Laravel PHP Example Javascript jQuery MORE

How to show total amount month and year wise Laravel


In this example we will discuss about how to show total amount month and year wise from database table using CodeIgniter framework PHP.

For show total amount month and year wise we uses 3 file here

  1. total_amount.blade.php
  2. TotalAmountController.php
  3. web.php

stotal_amount.blade.php

<!DOCTYPE html>
<html>
<head>
<title>Total Amount Calculation</title>
</head>
<body>
<table border = 1>
<tr>
<td> Year</td>
<td> Month</td>
<td> Amount</td>
</tr>
@foreach ($users as $user)
<tr>
<?php $monthNum = $user->month;
$dateObj = DateTime::createFromFormat('!m', $monthNum);
$monthName = $dateObj->format('F');?>
<td>{{ $user->year}}</td>
<td>{{ $monthName }}</td>
<td>{{ $user->total_amount }}</td>
</tr>
@endforeach
</table>
</body>
</html>

TotalAmountController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class TotalAmountController extends Controller {
public function store(){
$users = DB::select('select year(date) as year, month(date) as month, sum(paid) as total_amount from amount_table group by year(date), month(date)');
return view('total_amount',['users'=>$users]);
}
}

web.php

<?php
/* |--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
//delete data
Route::get('view-records','StudViewController@index');
Route::get('show','TotalAmountController@store');

Now run the program on your browser with the below URL:

This is my database table

After run the code i get the output

Output

Year Month Total Amount
2016 August 150
2017 August 150
2017 September 300

Thank you !