Monday, March 13, 2017

Laravel i and j dimensions

I have in my database a branches table where there are 2 branches. I have a second table for categories, where there are 4 categories. (NOT FIX NUMBERS, IT COULD CHANGE).

I'm trying to generate a table like this using angular js :

Branches| Food | Beverage | Tobaco | Disposable|
------------------------------------------------
Branch1 | 300  | 200      | 10     | 500       |
------------------------------------------------
Branch2 | 200  | 300      | 30     | 10        |

Here I have a table where i = categories and j=branches.

My Code :

$branches = DB::table('gc_branch')->where('BRAND_ID', $brandid)
                                  ->select('BRANCHID', 'BRANCHNAME')
                                  ->get();
$categories= DB::table('id_inventorycateg')->where('BRAND_ID', $brandid)
                                           ->select('CATID', 'CATNAME')
                                           ->get();

for ($i=0;$i<count($branches);$i++){
    for($j=0;$j<count($categories);$j++){
 $salesbycategory= 
 //QUERY
->select(DB::raw('sum(TOTAL+TAX1) AS TOTAL'), 'CATEGORYNAME')
->get();

$netsalesdata[]= ['BRANCHID' => $branches[$i]->BRANCHID, 'BRANCHNAME' =>$branches[$i]->BRANCHNAME, 'CATEGORYNAME' =>$salesbycategory[0]->$salesbycategory[0]->CATEGORYNAME, 'TOTAL' => $salesbycategory[0]->TOTAL];
}
}
return $netsalesdata;

I'm getting incorrect response :

[
{
"BRANCHID" :1,
"BRANCHNAME" : "branch1"
"CATEGORYNAME : "FOOD"
},
{
"BRANCHID" :1,
"BRANCHNAME" : "branch1"
"CATEGORYNAME : "BEVERAGE"
},
{
"BRANCHID" :1,
"BRANCHNAME" : "branch1"
"CATEGORYNAME : "TOBACO"
},
{
"BRANCHID" :1,
"BRANCHNAME" : "branch1"
"CATEGORYNAME : null
},
{
"BRANCHID" :2,
"BRANCHNAME" : "branch2"
"CATEGORYNAME : "FOOD"
},
{
"BRANCHID" :2,
"BRANCHNAME" : "branch2"
"CATEGORYNAME : "BEVERAGE"
},
{
"BRANCHID" :2,
"BRANCHNAME" : null
"CATEGORYNAME : null
},
{
"BRANCHID" :2,
"BRANCHNAME" : "branch2"
"CATEGORYNAME : null
},

AND that's incorrect for my table, can someone help me with this please, I tried everything I know, nothing works. Thank you



via Elio Chamy

Advertisement