Support the ongoing development of Laravel.io →
posted 9 years ago
Database
Last updated 1 year ago.
0

small tweak:

Schema::table('manufacturers', function($table)
{
    $table->string('name')->unique(); //notice the parenthesis I added
});
Last updated 1 year ago.
0

Based on the documentation (and this works for me too), http://laravel.com/docs/schema (under Adding Indexes section)

Schema::table('manufacturers', function($table)
{
    $table->unique('name'); //notice the parenthesis I added
});
Last updated 1 year ago.
0

Ah thanks! What an ass I am for not finding that...

Last updated 1 year ago.
0

No worries, defining a unique index is supported in Schema in several different ways, be it my way or shwetasabne's.

Last updated 1 year ago.
0

Right, you always got options in Laravel apparently. Thanks guys.

Now what would be the down function? How would you reverse this?

From what I gather you use drop_unique('name') in place of unique('name'), but I can't find that in the docs so I just want to confirm...

Thanks.

Last updated 1 year ago.
0

Sometimes you have to look into the API, when you don't see it in the docs. The schema command to drop a unique column is dropUnique.

Last updated 1 year ago.
0

That made an error when I refreshed my migrations..? (Before I'd just left my down function drop_unique('name'); commented out.)

Errors:

[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'name';
check that column/key exists (SQL: alter table manufacturers drop index n
ame)

[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'name';
check that column/key exists

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

chimmel chimmel Joined 4 Aug 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.