8

I'm using Laravel Query Builder to query 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 query gets Product which is existing in Store_Products for given store_id.

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?

Thank you very much in advance.

Zayn Ali
  • 4,765
  • 1
  • 30
  • 40
she hates me
  • 1,212
  • 5
  • 25
  • 44
  • Possible duplicate of [find() on model gives id as string in one environment and int in other](http://stackoverflow.com/questions/38034996/find-on-model-gives-id-as-string-in-one-environment-and-int-in-other) – Abraham Vegh Aug 08 '16 at 16:40

3 Answers3

8

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

Check whether mysqlnd is installed like so

$ sudo dpkg -l | grep 'mysqlnd'

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

$ sudo apt-get install php5-mysqlnd

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

linuxartisan
  • 2,396
  • 3
  • 21
  • 40
  • Can you explain why having `mysqlnd` installed will provide integers? – Jessedc Aug 08 '16 at 03:17
  • 1
    It's a native driver interface. Refer this for more info (http://stackoverflow.com/questions/38034996/find-on-model-gives-id-as-string-in-one-environment-and-int-in-other) – linuxartisan Aug 08 '16 at 03:22
4

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. Here it does not casts the id attribute to integer.

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.

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

Or you can cast your attribute

protected $casts = [
    'id' => 'integer',
];
Zayn Ali
  • 4,765
  • 1
  • 30
  • 40
0

This is somehow happens to my raw query doing SUM, IF, and other stuffs.

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.

So my workaround is to use 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.

Christhofer Natalius
  • 2,727
  • 2
  • 30
  • 39