I have one database table which contains multiple columns are there with different data types,one of the column is currency which is stored inside a database as an integer.
i have one API which is used for retrieving all the values from the database and displayed in UI,in my UI level they are accepting currency value as a string, is there any way to send the currency value as a string without changing the current structure of a database.,please give me some idea how to do this one..
CodePudding user response:
There are multiple ways. one is to use attribute casting
class User extends Model
{
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'currency' => 'string',
];
}
Another radical solution would be to remove the module mysqlnd so everything is returned as string.
CodePudding user response:
You can just use the strval() function of PHP
$currency = SomeModel::all()->map(function ($someRow) {
return $someRow->currrency = strval($someRow->currency);
}
Btw, The proper way to do this is using Accessors
public function getCurrency() {
return strval($this->currency);
}
Now, every time you get currency from your model, you will get the string casted value.
echo $model->currency;
// "10"
See the docs for more
