A better way to drop a unique index

It involves less guesswork

Joel Clermont
Joel Clermont

We always write and test the down method in our migrations. In fact, we did a whole podcast episode explaining why we do this. When doing this, you might need to drop an index before you can drop a column.

For example, the Laravel docs show you how to do this by passing the name of the index to the dropUnique method. But what is the name? There's a pattern Laravel follows, so it's not hard to calculate the name and include it as a string here.

But there is an even better way. The dropUnique method also accepts an array of column names. Using this approach, you can specify the exact same columns used in your up method. This works with other index types too, not just unique indexes.

I like the symmetry of this, and not having to calculate the index name manually. Why not leverage the framework to make our life easier and keep things consistent at the same time?

Here to help,


P.S. Download a collection of some of our favorite Laravel tips never published here. It's completely free, or you can pay a little something if you like.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼

Level up your Laravel skills!

Each 2-minute email has real-world advice you can use.