How to get Laravel Query Builder result as an integer

I use Laravel Query Builder to query a MySQL database, but it returns integer values ​​as string values.

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 request gets the Product that exists in Store_Productsfor the given store_id.

The problem is that it returns id(which is the main key for the product) as stringwhen I use Query Builder. There seems to be something wrong with the castes.

How can I solve this problem?

Thank you in advance.

+4
source share
2 answers

- , . mysqlnd.

, mysqlnd

$ sudo dpkg -l | grep 'mysqlnd'

, ( , php5)

$ sudo apt-get install php5-mysqlnd

ubuntu. - , .

+5

select $attribute , , , , MySQL , . id .

. (int) $variable " ", , .

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

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

Source: https://habr.com/ru/post/1650487/


All Articles