In my laravel project I am using repository pattern. I created following method to get brand by it's name from the brand table.
public function getBrandByName($name){
return $this->model->where('name','=',$name)->first();
}
In brand table I have record with name = "BrandOne". But when I call method with case sensitivity as getBrandByName('brandone') or getBrandByName('BrandOne') it both gives me the row.But I only need to get the record with case sensitivity. How can I do that?
Thank you
Case sensitivity is something that is controlled at database level. The collation that you use on your tables determines how characters are treated and ordered. A case-insensitive collation treats both upper and lowercase characters as the same.
Assuming you're using MySQL, if you have a collation that ends in _ci then it means you're using a case-insensitive collation.
You're able to set the collation you want to use in your Laravel database settings:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
You can change collation here to a case-sensitive collation and once your tables have been created with this new collation, MySQL will treat upper and lowercase characters differently.
Collations that end with _cs or _bin are usually case-sensitive.
Please note though, that this change will not take effect until you re-migrate your database (or change collation manually) as the collation is set when your tables are created.
It is possible to do a case-sensitive comparison using MySQL though, but I don't think it's supported out of the box by Laravel, so you'll have to use a raw query for it.
use DB::raw() method and then:
public function getBrandByName($name){
return $this->model->where(DB::raw("BINARY `name`"), $name)->first();
}
hope this helps
Related
I'm getting this error:
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value: '\xBD Inch...' for column 'column-name' at row 1
My database, table, and column have the format utf8mb4_unicode_ci also column-name is type text and NULL.
This is the value of the column-name
[column-name] => Some text before 11 ▒ and other text after, and after.
However I wait that laravel adds quotes to column's values, because the values are separated by commas (,). It should be as follow:
[column-name] => 'Some text before 11 ▒ and other text after, and after.'
See below the Schema
Schema::create('mws_orders', function (Blueprint $table) {
$table->string('custom-id');
$table->string('name');
$table->string('description')->nullable();
$table->string('comment')->nullable();
$table->integer('count')->nullable();
$table->text('column-name')->nullable();
$table->timestamps();
$table->primary('custom-id');
});
I have been looking for on google but not any solution, yet.
Anyone has an idea how to solve this issue?
I'm using Laravel 5.5 and MariaDB 10.2.11.
I solved it, encoding to uft-8 all string columns that generated this error before insert. For example, the column that generated the error was column-name, I encoded as show bellow. Also I found other column with the same error, I used this solution, too.
$data [
//key=>values
];
$myModel = new MyModel();
$data['column-name'] = DB::connection()->getPdo()->quote(utf8_encode($data['column-name']));
$myModel->insert($data);
I ran into similar problems with Laravel 5.5 and MariaDB 10.2. When storing some user input t into a varchar column, an exception:
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value: '\xE2\x80\x86y\xE2\x80...' for column 'comment' at row 1
will be thrown.
Since this only happened on the stage server but not on local dev server, I compared the collation and charset of underlining table, it turns out database and table on stage server use latin1 while local dev server uses utf8mb4.
The problem was solved by changing database and table collation and char set to utf8mb4 and utf8mb4_unicode_ci.
ALTER DATABASE <db_name> CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE <table_name> CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
For anyone who runs into this problem, please check collation and char set of your database and table. Chances are Laravel app is encoding with utf8 while databases uses something else.
If mb-convert-encoding or utf8-encode are not solving this problem for you, check if you're only using string functions in their multibyte variants.
e.g.
Instead of substr you must use mb_substr
Doc reference here: Multibyte String Functions
Written for future readers who might end up with my same problem :)
BD is the latin1 (and several others) encoding for ½ (one-half). The error message talks about storing that in a datetime. So, it sounds like there are at least two errors --
mismatch of CHARACTER SETs
poorly formulated query
You show us something about the CREATE TABLE, but why would "inches" be involved in that?
Just change database configuration (charset & collation) in
config/database.php
to:
'connections' => [
'mydb' => [
'driver' => 'mysql',
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8mb4', // **for emoticons**
'collation' => 'utf8mb4_unicode_ci', // **for emoticons**
],
]
follow this steps for solve problem
1- change CHARACTER and COLLATE`s table
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
2-change config /database.php file
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',//****make sure these configs are set
'collation' => 'utf8mb4_unicode_ci',//****make sure these configs are set
'prefix' => '',
'strict' => true,
'engine' => null,
],
3- run this command to regenerate config cache
php artisan config:cache
Have an issue with a project in php(laravel), when i bring info from a database(mysql) and it contains accents i.e é, á . Php ends up showing a weird question mark, or crashin
'charset' => 'utf8mb4' is set in the database.php
CHARACTER SET=utf8mb4 is set on every table on the mysql database
the original project had
'charset' => 'latin1' and showed problems, changing to utf8mb4 solved the problem on OSX(the test local host) but the problem didnt fix when uploaded to the server(linux)
The data comes bad before even trying to show:
$example= DB::connection('mysql_db')->select("SELECT * FROM test)
Log::error($example);
log shows the query gets me the question mark character
Found a similar issue in:
scandir issue with accents in Linux work fine in OSX
But i am not sure if the answer applies only to scan dir, or to any php-linux accent issue
I had the same issue and I manage to fix it by adding these lines in the config/database.php
'mysql' => [
[...]
'charset' => 'latin1',
'collation' => 'latin1_swedish_ci',
[...]
],
Finally made it work, had to set
'mysql' => [
[...]
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
[...]
],
On config/database.php
and set CHARACTER SET=utf8mb4; at the end of each Table in my sql file(works setting it to the whole database but another documents needed the database to be default latin1).
OSX overwrites the charset of the DB with the one you set on the connection, but apparently linux doesn't allow the same.
The same solution didnt work with utf8
Basically I want to write a script that could automatically copy a Drupal 7 settings.php file and search/replace the database name with a different one.
Before starting with sed or something like that I've just been trying to grep out the database name, just to get the regular expression down.
So far I've had no luck, my best guess has been grep "'database' =>" settings.php
That gives me the output:
* 'database' => 'databasename',
* 'database' => 'databasename',
* 'database' => 'databasename',
* 'database' => 'databasename',
* 'database' => '/path/to/databasefilename',
'database' => 'MY_DATABASE',
I just want the last line 'database' => 'MY_DATABASE', but really just the name so ideally the output would just be MY_DATABASE.
Any thoughts?
After some playing around I came up with this:
grep "'database' =>" settings.php | grep -v '^\ \*'
it gives me the output:
'database' => 'MY_DATABASE',
Not so elegant but it gave me some output I can work with.
Why doesn't laravel migrate store the euro sign when running a regular migrate from the command prompt?
$query = "INSERT INTO `currencies` (`code`, `name`, `symbol`) VALUES ('USD', 'US Dollar', '$'), ('EUR', 'Euro', '€');";
DB::statement($query);
But when I manually update the field it stores just fine.
The settings in my database.php are:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => '',
'strict' => false,
],
I'd check the encoding of your database connection.
In config/database.php, look for the charset and collation values - if they're not set to utf8 (or some variation thereof - the collation will be a longer string containing "utf8"), then the euro character is probably getting dropped somewhere between your application and the database.
From what I understand, charset controls the set of characters available within your SQL statements (including any parameters). If you attempt to use a character from a different charset, it will be dropped.
The collation controls the way characters are sorted and compared by the MySQL engine. If you attempt to sort a column which contains characters not included in the collation for that column, then they will be sorted incorrectly.
For more details on charset and collation, here is an explanation from the MySQL docs.
In my database user stores data in Japanese characters. When I query those data I did not get the data in Japanese characters but I got 入力ãªã— in stead. I don't know how to solve this. Below is my database configuration.
Yii::$app->components = [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=my_database',
'username' => '****',
'password' => '****',
'charset' => 'utf8',
],
];
If I remove 'charset' => 'utf8', I got ??????? in stead of strange characters above or Japanese characters. I work with Yii framework 2 and with its ActiveRecord. Anyone knows any solutions?
I'm willing to bet that your database or your tables have the wrong charset/Collation.
Check databases:
SELECT * FROM information_schema.SCHEMATA
Check tables:
SHOW FULL COLUMNS FROM table_name;
It's possible to convert existing data (from latin1 to utf8 for example) but not super-easy. I would say you'll have an much easier time just changing the default charset "How to make MySQL handle UTF-8 properly" and create new database/tables rather than convert existing data.