small tweak:
Schema::table('manufacturers', function($table)
{
$table->string('name')->unique(); //notice the parenthesis I added
});
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
});
Ah thanks! What an ass I am for not finding that...
No worries, defining a unique index is supported in Schema in several different ways, be it my way or shwetasabne's.
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.
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.
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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community