You can use a mutator on CashModel and append a new column which will hold a total of every row separately.
Controller:
public function index()
{
return CashModel::whereBetween('created_at', [
now()->locale('en')->startOfWeek()->subWeek(),
now()->locale('en')->endOfWeek()->subWeek()
])->get();
}
Model:
public $appends = ['total_sum'];
public function getTotalSumAttribute($value)
{
return $this->opening_balance +
$this->labour +
$this->rent +
$this->electricity +
$this->creditors +
$this->gst +
$this->insurance +
$this->direct_debits +
$this->other +
$this->total_amount;
}
Output Will be:
[
{
"id": 1,
"labour": 215,
"rent": 5412,
"electricity": 1652,
"creditors": 845,
"gst": 161,
"insurance": 332,
"direct_debits": 1552,
"other": 2165,
"total_amount": 464,
"created_at": "2020-11-15T00:00:00.000000Z",
"updated_at": "2020-11-25T00:00:00.000000Z",
"total_sum": 12798
},
{
"id": 6,
"labour": 771,
"rent": 2087,
"electricity": 1517,
"creditors": 760,
"gst": 2947,
"insurance": 103,
"direct_debits": 4915,
"other": 4720,
"total_amount": 2348,
"created_at": "2020-11-21T04:16:48.000000Z",
"updated_at": "2020-11-25T04:16:48.000000Z",
"total_sum": 20168
}
]