-1

I am using a Laravel query builder to search for categories. Here it is:

$array[] = $categories->where('pk_i_id', $category_id)->first();

I have to manually convert the category id to string, even though in database it is Integer type. Why do I have to do this?

Actually - I have to do this on Linux machine with Lamp stack installed. On Windows machine with Xampp it considers the column as integer as it should.

naneri
  • 3,771
  • 2
  • 29
  • 53
  • Can you please show the code where you get the category id and need to change it to a string as well? – Joel Hinz Jan 12 '16 at 17:50
  • perhaps you want the mysqlnd driver – lagbox Jan 12 '16 at 17:55
  • @JoelHinz I just write $category_id = (string) $category_id and then the query works. Otherwise it does not. – naneri Jan 12 '16 at 17:58
  • But how do you get the category id in the first place? – Joel Hinz Jan 12 '16 at 17:59
  • @JoelHinz who cares? I get it in multiple place , from request, from another class calling the method, from cron job, in PHP artisan tinker I can call the method directly. I just tested it for the first time on Linux and all methods broke because of that wrong type. – naneri Jan 12 '16 at 18:02
  • Because you haven't actually said what type the category id is. You said it's an integer in the database but we don't know what type it is in php. You may be solving the wrong problem when it could turn out to be something easily fixed elsewhere. – Joel Hinz Jan 12 '16 at 18:11
  • This most likely depends on your MySQL driver. Check out this answer for more info: http://stackoverflow.com/questions/20079320/php-pdo-mysql-how-do-i-return-integer-and-numeric-columns-from-mysql-as-int – Thomas Kim Jan 12 '16 at 18:12
  • I'd also like to leave a link to this discussion here: http://stackoverflow.com/questions/5323146/mysql-integer-field-is-returned-as-string-in-php – Daniel Setréus Jan 12 '16 at 18:24

1 Answers1

1

The issue sounds more like that $categories is already a Collection, not a Builder. Both the Collection and the Builder have a where() method, but their logic is not the same.

The where() method on the Builder will add a parameterized where clause to the query run against the database. In this case, the type of variable doesn't matter.

However, the where() method on the Collection will loop through the collection and return those results where the field in the first parameter is strictly equal (===) to the value passed in the second parameter. To change this, you can pass false as the third parameter, and it will use a loose comparison (==) instead of strict. Additionally, you could use whereLoose(), which is a shortcut for where() with the third parameter as false.

$array[] = $categories->where('pk_i_id', $category_id, false)->first();
// or
$array[] = $categories->whereLoose('pk_i_id', $category_id)->first();

If the incorrect field types are causing more issues that what you've described, then you may want to work on fixing the underlying issue. As has been pointed out in the linked posts, on your LAMP stack you need to replace the mysqld driver with the mysqlnd driver.

patricus
  • 59,488
  • 15
  • 143
  • 145