Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to cast the value from the retrieved value from Database in laravel?

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..

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading