gotchaphplaravelModerate
Laravel Migration Column Modifier Order Matters
Viewed 0 times
migrationschema buildernullabledefaultafterdropColumnrollbackpretend
Error Messages
Problem
Adding a nullable() or default() modifier after after() or other positional modifiers sometimes produces unexpected SQL or an error, especially on MySQL. Developers also run php artisan migrate on production without a rollback plan.
Solution
Always call nullable() before default() in the chain. Place after() last. Always write a down() method that exactly reverses the up() method. Use php artisan migrate --pretend to preview SQL before running on production.
Why
Laravel's Blueprint builds the column DDL from the modifier chain in order. Some drivers (MySQL) are strict about modifier ordering. A missing or broken down() makes rollback impossible.
Gotchas
- dropColumn() on SQLite requires doctrine/dbal pre-Laravel 10 but is native in Laravel 10+
- Renaming a column with renameColumn() requires doctrine/dbal on MySQL older than 8.0
- Squash migrations with php artisan schema:dump to keep migrations folder clean on large projects
- Never edit an already-run migration; create a new one instead
Code Snippets
Correct modifier ordering
$table->string('bio')->nullable()->default('')->after('email');Revisions (0)
No revisions yet.