jeudi 24 janvier 2019

How to turn string output from sql to integer?

I am generating charts using google chart api. I have done it locally and successfully produce the charts. However, when I transfer them to server (cpanel) the charts is ruined. I have detected that the output of sql query for generating the charts value is produced in string format with "6".

This is the code to get the charts' value:

   public function gender()
{
$data = DB::table('results')
   ->select(
    DB::raw('gender as gender'),
    DB::raw('count(*) as number'))
   ->groupBy('gender')
   ->get();
   //dd($data);
 $array[] = ['Gender', 'Number'];
 foreach($data as $key => $value)
 {
  $array[++$key] = [$value->gender, $value->number];
 }
 //dd($array);
 return view('gender')->with('gender', json_encode($array));
}

In my local, I try to access the data of the sql using dd($data); producing:

 Collection {#263 ▼
 #items: array:2 [▼
 0 => {#264 ▼
  +"gender": "female"
  +"number": 6
}
1 => {#266 ▼
  +"gender": "male"
  +"number": 6
}
]
}

I tried accessing in server using same dd($data);

  Collection {#260 ▼
#items: array:2 [▼
0 => {#261 ▼
  +"gender": "female"
  +"number": "6"
}
1 => {#263 ▼
  +"gender": "male"
  +"number": "6"
}
]

The difference is the number value from server code is in string.

Why this happened and how to fix the problem?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire