简体   繁体   English

如何获得 Laravel Query Builder 结果为 integer

[英]How to get Laravel Query Builder result as integer

I'm using Laravel Query Builder to query MySQL database but it returns integer values as string values.我正在使用 Laravel 查询生成器查询 MySQL 数据库,但它返回 integer 值作为字符串值。

I have the following query.我有以下查询。

$query = DB::table('store_products')->select('products.id', 'products.name', 'products.unit_type', 'products.price', 'products.image_path', 'products.is_popular', 'store_products.price AS store_price')
           ->join('products', 'products.id', '=', 'store_products.product_id')
           ->join('product_categories', 'product_categories.product_id', '=', 'store_products.product_id')
           ->where('store_products.store_id', $store_id)
           ->where('store_products.product_id', $product_id);

Here the query gets Product which is existing in Store_Products for given store_id .此处查询获取给定store_idStore_Products中存在的 Product。

The problem is, it returns id (which is the Primary Key for Product) as string when I use Query Builder.问题是,当我使用查询生成器时,它以string形式返回id (这是产品的主键)。 Looks like there is something wrong with casts.看起来演员表有问题。

How can I solve this problem?我怎么解决这个问题?

Thank you very much in advance.非常感谢你提前。

Casting is not a solution but a workaround to the problem. 转换不是解决方案,而是解决问题的方法。 Your actual problem is missing mysqlnd plugin. 你的实际问题是缺少mysqlnd插件。

Check whether mysqlnd is installed like so 检查是否安装了mysqlnd

$ sudo dpkg -l | grep 'mysqlnd'

If it's not installed, you need to install it like so (assuming you have php5) 如果没有安装,你需要安装它(假设你有php5)

$ sudo apt-get install php5-mysqlnd

These commands are for ubuntu . 这些命令适用于ubuntu If you have something else, just convert them to your appropriate OS. 如果您还有其他内容,只需将它们转换为适当的操作系统即可。

When fetching by select it populates the $attribute internal property with raw data returned by the underlying driver, so generally the MySQL driver is configured to return all columns as strings. 当通过select获取时,它会使用底层驱动程序返回的原始数据填充$attribute内部属性,因此通常将MySQL驱动程序配置为将所有列作为字符串返回。 Here it does not casts the id attribute to integer. 这里它不会将id属性强制转换为整数。

You have to manually cast it to integer. 您必须手动将其转换为整数。 you can either use (int) $variable syntax to cast it to integer on the fly where you are accessing the attribute of the model or you can make a mutator for that reason. 您可以使用(int) $variable语法将其转换为动态的整数,您可以在其中访问模型的属性,或者您可以为此创建一个mutator。

public function getIdAttribute($value)
{
    return (int) $value;
}

Or you can cast your attribute 或者你可以投射你的属性

protected $casts = [
    'id' => 'integer',
];

This is somehow happens to my raw query doing SUM, IF, and other stuffs.这不知何故发生在我做 SUM、IF 和其他东西的原始查询中。

But I don't want to add more codes in model for accessor or casts like Zayn Ali's answer just for this particular query.但我不想在 model 中为访问器或强制转换添加更多代码,例如 Zayn Ali 针对此特定查询的回答。

So my workaround is to use MySQL cast function所以我的解决方法是使用MySQL cast function

$query->selectRaw('CAST(column_1 AS UNSIGNED) AS col1')

Just make sure you are aware of the SQL injection when using raw query.使用原始查询时,请确保您了解 SQL 注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM