問題描述
我使用 Laravel Query Builder 查詢 MySQL 數(shù)據(jù)庫,但它返回整數(shù)值作為字符串值.
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
.
問題是,當(dāng)我使用查詢生成器時,它返回 id
(這是產(chǎn)品的主鍵)作為 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?
非常感謝您.
推薦答案
強(qiáng)制轉(zhuǎn)換不是解決方案,而是解決問題的方法.您的實際問題是缺少 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'
如果沒有安裝,你需要像這樣安裝(假設(shè)你有php5)
If it's not installed, you need to install it like so (assuming you have php5)
$ sudo apt-get install php5-mysqlnd
這些命令適用于 ubuntu.如果您還有其他東西,只需將它們轉(zhuǎn)換為您合適的操作系統(tǒng)即可.
These commands are for ubuntu. If you have something else, just convert them to your appropriate OS.
這篇關(guān)于如何將 Laravel Query Builder 結(jié)果作為整數(shù)獲取的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!