TOPIC COLLECTION
Migrations.
A collection of 16 practical ideas to make everyday Laravel work a little smoother.
16 TIPS01Order of MigrationsIf you want to change the order of DB migrations, just rename the file's timestamp, like from 2018 08 04 070443 create posts table.php to 2018 07 04 070443…Migrations02Migration Fields with TimezonesDid you know that in migrations there's not only timestamps() but also timestampsTz() , for the timezone?Migrations03Database Migrations Column TypesThere are interesting column types for migrations, here are a few examples. See all column types on the official documentation.Migrations04Default TimestampWhile creating migrations, you can use timestamp() column type with option useCurrent() and useCurrentOnUpdate() , it will set CURRENT TIMESTAMP as default…Migrations05Migration StatusIf you want to check what migrations are executed or not yet, no need to look at the database "migrations" table, you can launch php artisan migrate:status…Migrations06Add Composite Indexes for Common Query FiltersIndex columns that appear together in frequent filters, joins, or sort orders after checking the actual query plan. A composite index can help a query that…Migrations07Create Migration with SpacesWhen typing make:migration command, you don't necessarily have to use underscore symbol between parts, like create transactions table . You can put the name…Migrations08Create Column After Another ColumnNotice: Only MySQL If you're adding a new column to the existing table, it doesn't necessarily have to become the last in the list. You can specify after…Migrations09Make Migration for Existing TableIf you make a migration for existing table, and you want Laravel to generate the Schema::table() for you, then add "\ in xxxxx table" or "\ to xxxxx table"…Migrations10Output SQL Before Running MigrationsWhen typing migrate --pretend command, you get the SQL query that will be executed in the terminal. It's an interesting way to debug SQL if necessary.Migrations11Anonymous MigrationsThe Laravel team released Laravel 8.37 with anonymous migration support, which solves a GitHub issue with migration class name collisions. The core of the…Migrations12You Can Add "Comment" About a Column Inside Your MigrationsYou can add "comment" about a column inside your migrations and provide useful information.Migrations13Checking for Table / Column ExistenceYou may check for the existence of a table or column using the hasTable and hasColumn methodsMigrations14Group Columns Within an After MethodIn your migrations, you can add multiple columns after another column using the after methodMigrations15Add the Column in the Database Table Only If It's Not Present & Can Drop It If, Its PresentNow you can add the column in the database table only if its not present & can drop it if, its present. For that following methods are introducedMigrations16Method to Set the Default Value for Current TimestampYou can use useCurrent() method for your custom timestamp column to store the current timestamp as a default value.Migrations