// After columns are created...
$table->primary('primary_id'); // Primary key; unnecessary if used increments()
$table->primary(['first_name', 'last_name']); // Composite keys
$table->unique('email'); // Unique index
$table->unique('email', 'optional_custom_index_name'); // Unique index
$table->index('amount'); // Basic index
$table->index('amount', 'optional_custom_index_name'); // Basic index
注意第一个例子,primary并不是必须的,如果你使用的是increments() or bigIncrements()来创建索引,那么会自动添加自增主键
删除索引
示例5-4显示如何删除索引
Example 5-4. Removing column indexes in migrations
$table->dropPrimary('contacts_id_primary');
$table->dropUnique('contacts_email_unique');
$table->dropIndex('optional_custom_index_name');
// If you pass an array of column names to dropIndex, it will // guess the index names for you based on the generation rules
$table->dropIndex(['email', 'amount']);