Nov 20, 2022

# Solution 1 :

You can calculate the sum in a query. Using SUM function in select.

Eg:

`````` DB::raw("SUM(marks) as total_marks")
``````

# Problem :

I already view the data from the database and filter the data. Right now, I want to do the calculation but I didn’t find a way to do calculation inside the HTML itself. Here’s my html coding. Should I do the javascript function here? or how? I confused on how to do the calculation based on the array of the table.

``````@extends('admin.layouts.app')

@section('content')

<style>
tfoot {
background-color: #a8d1e3;
}
</style>

<script>
console.log("try");

</script>

<div class="container">

<div class="card">
<h3>Total Sales</h3>
</div>
<div class="card-body">
@csrf
<label>Filter by date : </label>
<div class="row">
<div class="col-md-2">
<div class="form-group">
<input id="from" name="from" value="{{\$from}}" placeholder="From" type="text"
class="form-control datepicker">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<input id="to" name="to" value="{{\$to}}" placeholder="To" type="text"
class="form-control datepicker">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<select class="form-control" name="insurance_id">
<option value=-1">ALL</option>
@foreach (\$insurances as \$item)
<option value="{{\$item->id}}">{{\$item->name}}</option>
@endforeach
</select>
</div>
</div>
<div class="col-md-2">

<div class="form-group">
<select class="form-control" name="exported">
<option value="-1">ALL</option>
<option value="1">CHECKED</option>
<option value="2">UNCHECKED</option>
</select>
</div>
</div>
<div class="col-md-3">
<button type="submit" class="btn btn-success btn-sm pr-5 pl-5">Search</button>
</div>
<div class="col-md-4">
</div>
</div>
</form>
<div class="table-responsive">
<table class="table table-striped">
<tr>
<th scope="col">#</th>
<th scope="col">Policy No</th>
<th scope="col">Product Name</th>
{{-- <th scope="col">Region</th> --}}
<th scope="col">Package</th>
<th scope="col">Plan</th>
<th scope="col">Enroll Date</th>
<th scope="col">Departure</th>
<th scope="col">Return</th>
<th scope="col">Price</th>
<th scope="col">Exported</th>
<th scope="col"></th>
</tr>
<tbody>
<?php \$count = 1; ?>
@foreach (\$items as \$item)

<tr>
<th scope="row">{{\$count++}}</th>
<td>{{\$item->policy_no}}</td>
<td>{{\$item->insurance->name}}</td>
{{-- <td>{{\$item->region->name}}</td> --}}
<td>{{\$item->insurance->name}}</td>
<td>{{\$item->plan->name}}</td>
<td>{{\$item->created_at}}</td>
<td>{{\$item->depart_date}}</td>
<td>{{\$item->return_date}}</td>
<td>RM {{number_format(\$item->amount,2)}}</td>
<td>
@if (\$item->exported == 0)
<strong>NO</strong>
@else
<strong>YES</strong>
@endif
</td>

<td>
<a class="btn btn-xs btn-success mb-1 btn-block text-white"
</td>
@endforeach
</tbody>
</table>
</div>
</div>
<div class=" card-footer">
@csrf
<label for="TotalSales" align="right">Total Sales: </label>
<input id="TotalSales"><br>
</div>
<div class=" card-footer">
@csrf
<input id="from1" value="{{\$from}}" name="from1" type="hidden">
<input id="to1" value="{{\$to}}" name="to1" type="hidden">
<input id="insurance_id" value="{{\$insurance_id}}" name="insurance_id" type="hidden">
<input id="exported" value="{{\$exported}}" name="exported" type="hidden">
<button type="submit" class="btn btn-primary">Export to CSV</button>
<a href="/admin/insurance/product/mark-exported" class="btn btn-success">Mark as Exported</a>
<a href="/admin/insurance/product/benefit" class="text-white btn btn-secondary">Generate Report</a>
</form>
</div>
</div>
</div>
@endsection

``````

Here’s the controller..

``````
public function filterDate(Request \$request)
{
\$insurances = Insurance::all();
\$from = \$request->from;
\$to = \$request->to;
\$insurance_id = \$request->insurance_id;
\$exported = \$request->exported;
\$enrollments = InsuranceEnrollment::where('status', '=', 2)
->where("insurance_id", \$insurance_id)
->whereBetween('created_at', [\$from, \$to])
->orderBy('created_at', 'DESC')
->paginate(10);

\$items = array(
'items' => \$enrollments,
'insurances' => \$insurances,
'insurance_id' => \$insurance_id,
'exported' => \$exported,
'from' => \$from,
'to' => \$to
);

}

``````

### Comment posted by TsaiKoga

why don’t you just calculate in controller action, and just render it.