Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel eloquent integers returned as strings in mssql

i am running mssql2008 with laravel 5.0 and every time i retrieve data from DB seems like everything is been returned as string no matter which format i have it stored on the database.

{
    id: "1",
    name: "This is crazy",
    owner_id: null,
    is_local: "0",
    vessel_type_id: "3",
    registration_number: "34535",
    gross_register_tonnage: "34",
    year_built: "3453",
    status: "active",
    deleted_at: null,
    created_at: "Nov 5 2014 03:25:38:577PM",
    updated_at: "Nov 17 2014 09:06:40:000AM"
}

i know we can use eloquent mutators to format fields separately but that's no way to do this as there are multiple integer fields and i don't wanna create a function for each and every field.

this is not a problem with laravel Response::json i have dumped the array using var_dump and seems all fields are been returned as strings.

i have had this problem before but that was with mysql and it turned out it was an issue with mysql drivers had to install php5_mysqlnd.

i am using Ubuntu 14.04 .

like image 714
muhamin Avatar asked Nov 17 '14 14:11

muhamin


1 Answers

I use MySQL and also found same problem in some cases. Don't use mutator, use attribute casting instead.

protected $casts = [
    'id' => 'integer',
    'owner_id' => 'integer',
    'is_local' => 'integer',
    'vessel_type_id' => 'integer',
    'registration_number' => 'integer',
    'gross_register_tonnage' => 'integer',
    'year_built' => 'integer',
];
like image 135
PaePae Avatar answered Oct 29 '22 19:10

PaePae