如何获取Laravel Query Builder的结果为整数 [英] How to get Laravel Query Builder result as integer
问题描述
我使用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
给定 store_id
。
Here the query gets Product which is existing in Store_Products
for given store_id
.
问题是,它返回 id $当我使用Query Builder时,c $ c>(它是产品的主键)作为
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.
如何解决这个问题?
非常感谢提前。
推荐答案
铸造不是解决方案,而是解决问题的解决方法。您的实际问题是缺少 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.
这篇关于如何获取Laravel Query Builder的结果为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!