I need to save numbers(basically cost) of per API call in mySQL database, For example I am getting a response from API as follows:
$object = stdClass Object
(
[id] => 2457457845
[price] => -0.00870 /// In USD
)
Now I want to save the call cost in mysql database.
My column type is decimal(5,5).
My Table Structure
+-----------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+--------------+------+-----+---------+----------------+
| id | int unsigned | NO | PRI | NULL | auto_increment |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | | |
| call_start_date_utc | timestamp | NO | | NULL | |
| call_end_date_utc | timestamp | NO | | NULL | | |
| call_duration | int | NO | | NULL | |
| call_price | decimal(5,5) | NO | | NULL | |
| call_forwarded_from | varchar(50) | YES | | NULL | |
+-----------------------+--------------+------+-----+---------+----------------+
I try save it to database in the following manner:
$tcd = new CallData;
$tcd->created_at = Carbon::now();
$tcd->updated_at = Carbon::now();
$tcd->call_start_date_utc = date('Y-m-d H:i:s', strtotime($call_data->startTime->format('Y-m-d H:i:s')));
$tcd->call_end_date_utc = date('Y-m-d H:i:s', strtotime($call_data->endTime->format('Y-m-d H:i:s')));
$tcd->call_duration = $call_data->duration;
$tcd->call_price = ($call_data->price > 0) ? $call_data->price : abs($call_data->price);
$tcd->call_forwarded_from = $call_data->forwardedFrom;
$tcd->save();
I tried change the decimal
type of column call_price
to varchar(50)
in database still it saves as 0. I am using abs()
to convert negative to positive(just for display purpose).
I just want to save it as 0.00870
in the database. Can anyone guide me please?