如何将 Laravel Query Builder 结果作为整数获取

2021-12-26 00:00:00 php laravel laravel-5 eloquent laravel-5.2

我使用 Laravel Query Builder 查询 MySQL 数据库,但它返回整数值作为字符串值.

I'm using Laravel Query Builder to query MySQL database but it returns integer values as string values.

我有以下查询.

$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);

此处查询获取存在于 Store_Products 中的 Product,用于给定 store_id.

Here the query gets Product which is existing in Store_Products for given store_id.

问题是,当我使用查询生成器时,它返回 id(这是产品的主键)作为 string.演员表好像有问题.

The problem is, it returns id (which is the Primary Key for Product) as string when I use Query Builder. Looks like there is something wrong with casts.

我该如何解决这个问题?

How can I solve this problem?

非常感谢您.

推荐答案

强制转换不是解决方案,而是解决问题的方法.您的实际问题是缺少 mysqlnd 插件.

Casting is not a solution but a workaround to the problem. Your actual problem is missing mysqlnd plugin.

检查mysqlnd是否像这样安装

$ sudo dpkg -l | grep 'mysqlnd'

如果没有安装,你需要像这样安装(假设你有php5)

If it's not installed, you need to install it like so (assuming you have php5)

$ sudo apt-get install php5-mysqlnd

这些命令适用于 ubuntu.如果您还有其他东西,只需将它们转换为您合适的操作系统即可.

These commands are for ubuntu. If you have something else, just convert them to your appropriate OS.

相关文章