Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

laravel - Why PHP considers MySQL INT columns as strings?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...