Home >Backend Development >PHP Tutorial >How to implement case sensitivity in laravel query database?
There is a field in the 'user' table called 'account'. Now we need to use the laravel ORM method to query this field,
For example: There are three pieces of data in the user table. The values of the account field are: abc, AbC, ABC. Now use 'User::where("account","ABC")->get()' to query , these three pieces of data should be obtained, but what I want is to only get the piece of data with the account value ABC. How to achieve this? ? The database is mysql
There is a field in the 'user' table called 'account'. Now we need to use the laravel ORM method to query this field,
For example: There are three pieces of data in the user table. The values of the account field are: abc, AbC, ABC. Now use 'User::where("account","ABC")->get()' to query , these three pieces of data should be obtained, but what I want is to only get the piece of data with the account value ABC. How to achieve this? ? The database is mysql
Add binary to the corresponding fields of the MySQL table
http://cevin.net/archives/ to make mysql queries case-sensitive.html
@cevin is talking about the method of distinguishing and directly using binary types when querying. Personally I don't think it's appropriate. It is especially bad if you do not design constraints when creating a table and force binary comparison when querying. This will cause a lot of judgments in your code and may be bypassed. As for using binary fields directly, mysql will not treat them as characters. I think there may be problems. My solution is given below.
We commonly use table creation statements like this. Its collate has been declared as ci (case insensitive)
<code> `account` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL</code>
We just need to write it like this to make it case sensitive.
<code> `account` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,</code>
It has nothing to do with laravel. Mysql table setting problem
PHP7 online compilation and execution