0

Please need help...

$months = Records::select('payment_month')
    ->distinct()->where('payment_year',$year)
    ->where('company_id',$company_id)->get();

foreach ($months as $month) {

    $data = Records::select(
        DB::raw('SUM(record_db.credits) as credits'),
        DB::raw('SUM(record_db.amount) as amount'),
        DB::raw('SUM(record_db.totalamount) as totalamt'), 'record_db.payment_month')
            ->where('record_db.company_id','=',$company_id)
            ->where('record_db.payment_month','=',$month->payment_month)
            ->where('record_db.payment_year','=',$year)
            ->first();
}       

return Response::json($data);

The above query works fine and I have the month of January, February and March in the database table but it only returns the records of March.

I tried results[]=$data but still don't work.

2
  • What result do you want to have exactly? Commented May 15, 2014 at 10:16
  • January 910 345 657 February 345 980 123 March 214 436 989, something like this... Commented May 16, 2014 at 5:01

2 Answers 2

1

I think this is what you need:

$data = Records::select(
    'payment_month',
    DB::raw('SUM(record_db.credits) as credits'),
    DB::raw('SUM(record_db.amount) as amount'),
    DB::raw('SUM(record_db.totalamount) as totalamt')
)->where('company_id', '=', $company_id)
->where('payment_year', '=', $year)
->groupBy('payment_month')
->orderBy('payment_month')
->get();

// then
$data->toJson(); // returns something like:
[{"payment_month":"January","credits":"123","amount":"456","totalamt":"789"}, ... ]
Sign up to request clarification or add additional context in comments.

1 Comment

Was able to solve it when the site was in read only mode, using $result[]=$data->toArray(). Thanks...
0

You had override $data array from every month result. You should use whereIn as follows.

$months = Records::select('payment_month')
    ->distinct()->where('payment_year', $year)
    ->where('company_id',$company_id)->get();

$monthValues = array();

foreach ($months as $month) {
    $monthValues[] = $month->payment_month;
}

$data = Records::select(
    DB::raw('SUM(record_db.credits) as credits'),
    DB::raw('SUM(record_db.amount) as amount'),
    DB::raw('SUM(record_db.totalamount) as totalamt'), 'record_db.payment_month')
    ->where('record_db.company_id','=',$company_id)
    ->whereIn('record_db.payment_month', $monthValues)
    ->where('record_db.payment_year','=',$year)
    ->first();

5 Comments

$monthValues is returning null
check your $months array. It should null.
Months array contain payment_month: January, payment_month: February, payment_month: March
So. how $monthValues be null? check print $month array in the loop. Anyway you should to do whereIn. Check it with laravel manual.
sorry, I was doing a typo but using whereIn is giving errors

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.