0

I am having data : enter image description here

I want to get result for sum of each row by perticular created_at. For example, sum of labour,rent etc. on date of "2020-10-12".

I tried query which gives me data of perticular week :

$cashFlowDetails =  CashModel::whereBetween('created_at', [
            now()->locale('en')->startOfWeek()->subWeek() ,
            now()->locale('en')->endOfWeek()->subWeek() ]) 
             ->get();
Damini Suthar
  • 1,470
  • 2
  • 14
  • 43

3 Answers3

1

As far as I understand you need the date wise totals for each of labour, electricity and so on.

Below query should get you the desired results.

$cashFlowDetails = Cash::groupBy('created_at')
    ->selectRaw(
        'created_at,
         sum(labour) as labour,
         sum(rent) as rent,
         sum(electricity) as electricity,
         sum(creditors) as creditors,
         sum(gst) as gst,
         sum(insurance) as insurance,
         sum(direct_debits) as direct_debits,
         sum(others) as others, 
         sum(total_amount) as total_amount',        
    )
    ->whereBetween('created_at', [
        now()->locale('en')->startOfWeek()->subWeek() ,
        now()->locale('en')->endOfWeek()->subWeek() ])
    ->get();
Donkarnash
  • 12,433
  • 5
  • 26
  • 37
0

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
    }
]
Mudit Gulgulia
  • 1,131
  • 7
  • 21
0

SUM(larbour) as sum_labour, SUM(rent) as sum_rent, etc.

DB::table('table_name')
  ->select(DB::raw('SUM(labour) as sum_labour, DATE(created_at) as created_date'))
  ->groupBy(DB::raw('DATE(created_at)'))
  ->get();