0

I have 1 brand and 2 branches in my database, I'm getting the sales data for each branch.

public function getCurrentSales($brandid){
$branches = DB::table('gc_branch')->where('BRAND_ID', $brandid)
                                  ->select('BRANCHID', 'BRANCHNAME')
                                  ->get(); 

for ($i=0; $i<count($branches);$i++){
$mtdnetsales= DB::table('st_sales')
//query
->select(DB::raw('sum(AMOUNT) as TOT')->get();

$ytdnetsales= DB::table('st_sales')
//query
->select(DB::raw('sum(AMOUNT) as TOT')->get();


$netsalesdata=[['BRANCHID' => $branches[$i]->BRANCHID, 'BRANCHNAME' =>branches[$i]->BRANCHNAME, 'MTDNETSALES' =>$mtdnetsales[0]->TOT, 'YTDNETSALES' =>$ytdnetsales[0]->TOT]];

}//end for

return $netsalesdata;

My problem is :

  • if I put the return $netsalesdata in the for loop, I get the first raw only (1 branch only)
  • if I put it outside the loop, i get the last row(the second branch only) , while my database has 2 branches
1
  • if you dd($branches); do you get both branches? Commented Mar 6, 2017 at 10:52

3 Answers 3

1

Change your netstellar to this (and keep it inside of for loop) :

$netsalesdata[$i]=[['BRANCHID' => $branches[$i]->BRANCHID, 'BRANCHNAME' =>branches[$i]->BRANCHNAME, 'MTDNETSALES' =>$mtdnetsales[0]->TOT, 'YTDNETSALES' =>$ytdnetsales[0]->TOT]];

and return this :

return $netsalesdata[];
Sign up to request clarification or add additional context in comments.

Comments

0
public function getCurrentSales($brandid) {
    $branches = DB::table('gc_branch')->where('BRAND_ID', $brandid)
                  ->select('BRANCHID', 'BRANCHNAME')->get(); 

    for ($i=0; $i<count($branches);$i++){
        $mtdnetsales= DB::table('st_sales')
               ->select(DB::raw('sum(AMOUNT) as TOT')->get();

        $ytdnetsales= DB::table('st_sales')
               ->select(DB::raw('sum(AMOUNT) as TOT')->get();

        $netsalesdata[] =[
            'BRANCHID' => $branches[$i]->BRANCHID,
            'BRANCHNAME' =>branches[$i]->BRANCHNAME,
            'MTDNETSALES' =>$mtdnetsales[0]->TOT,
            'YTDNETSALES' =>$ytdnetsales[0]->TOT];

    }//end for

   // get size of the array
   $records = count($netsalesdata);
   // To get last record
   print_r($netsalesdata[$records -1]);
}

Comments

0

Use array_push function to append new variables:

public function getCurrentSales($brandid){
    $netsalesdata= [];
    $branches = DB::table('gc_branch')->where('BRAND_ID', $brandid)
                                      ->select('BRANCHID', 'BRANCHNAME')
                                      ->get(); 

    for ($i=0; $i<count($branches);$i++){
        $mtdnetsales= DB::table('st_sales')
        //query
        ->select(DB::raw('sum(AMOUNT) as TOT')->get();

        $ytdnetsales= DB::table('st_sales')
        //query
        ->select(DB::raw('sum(AMOUNT) as TOT')->get();


        array_push($netsalesdata, ['BRANCHID' => $branches[$i]->BRANCHID, 'BRANCHNAME' =>branches[$i]->BRANCHNAME, 'MTDNETSALES' =>$mtdnetsales[0]->TOT, 'YTDNETSALES' =>$ytdnetsales[0]->TOT]);

     }//end for

     return $netsalesdata;
}

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.