Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Laravel Query Builder result as integer

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.

like image 926
she hates me Avatar asked Aug 07 '16 23:08

she hates me


People also ask

How do I get the query builder to output its raw SQL query as a string?

DB::QueryLog() works only after you execute the query using $builder->get() . If you want to get the raw query before or without executing the query, you can use the $builder->toSql() method.

What does get () do in Laravel?

This allows you to add conditions throughout your code until you actually want to fetch them, and then you would call the get() function.

Which is better eloquent or query builder in Laravel?

Eloquent ORM is best suited working with fewer data in a particular table. On the other side, query builder takes less time to handle numerous data whether in one or more tables faster than Eloquent ORM. In my case, I use ELoquent ORM in an application with tables that will hold less than 17500 entries.

What is toArray in Laravel?

toArray is a model method of Eloquent, so you need to a Eloquent model, try this: User::where('name', '=', 'Jhon')->get()->toArray(); http://laravel.com/docs/eloquent#collections. Follow this answer to receive notifications.25-Dec-2013.


2 Answers

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.

like image 79
linuxartisan Avatar answered Sep 21 '22 22:09

linuxartisan


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',
];
like image 24
Zayn Ali Avatar answered Sep 21 '22 22:09

Zayn Ali