laravel雄辩的整数返回为mssql中的字符串 [英] laravel eloquent integers returned as strings in mssql
问题描述
我正在使用laravel 5.0运行mssql2008,每次我从DB中获取数据
似乎一切都作为字符串返回,无论我将数据存储在数据库中。
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.
这不是laravel的问题Response :: json我已经使用var_dump转储了数组,似乎所有字段都作为字符串返回。
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.
我以前有这个问题,但是这是与mysql,事实证明这是一个问题,mysql驱动程序不得不安装php5_mysqlnd。
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.
我正在使用Ubuntu 14.04。
i am using Ubuntu 14.04 .
推荐答案
我使用MySQL,在某些情况下也发现了同样的问题。不要使用mutator,请改用属性转换。
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',
];
这篇关于laravel雄辩的整数返回为mssql中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!