CriticalTheWizard CriticalTheWizard - 4 years ago 199
MySQL Question

Laravel Schema Builder | Unique, Case Sensitive Column

I'm using Laravel's schema builder with mysql to make a unique column. But when I use the unique method it is case insensitive. I need it to be case sensitive. How can I do that?


Schema::create('item', function (Blueprint $table) {

First entry into database:

$i = new Item;
$i->key = "Random_Key";

Second entry into database (returns duplicate entry error):

$i = new Item;
$i->key = "random_key";

Answer Source

You need to use character sets and collations for specifying case sensitive columns in mySQL

Laravel has the collate and charset column modifiers in mySQL for this purpose

So, you may use something like: $table->string('key')->charset('utf8')->collate('utf8_cs')->unique()

As the OP stated, here's what worked for him:

$cs = $table->string('key')->unique();

$cs->collation = 'utf8_bin';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download