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();
8
  • I sholud get result by query or by using foreach loop? Commented Nov 25, 2020 at 3:04
  • You may try something like CashModel::whereDate('created_at', '2020-10-12')->sum('labour'); Commented Nov 25, 2020 at 3:09
  • what if I wants to get sum of all rows? like labour,rent,electricity and so on Commented Nov 25, 2020 at 3:12
  • Can you upload a sql dump of your table with small set of data somewhere from where I can download - then I can try and write the query for your desired output. As such you can do individual query for each of labour, rent, electricity and so on Commented Nov 25, 2020 at 3:18
  • @DaminiSuthar you already asked this question i guess Commented Nov 25, 2020 at 3:39

3 Answers 3

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();
Sign up to request clarification or add additional context in comments.

Comments

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
    }
]

Comments

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.