Appearance
Laravel migration table field's type change
To change the type of a field in a table using a migration in Laravel, you can use the change method on the Schema facade. For example, to change the type of a field named "email" in a table named "users" from a string to an integer, you can use the following code in your migration file:
Example of using the change method on the Schema facade to change the type of a field in a table using a migration in Laravel
php
Schema::table('users', function (Blueprint $table) {
$table->integer('email')->change();
});
<div class="alert alert-info flex not-prose">Watch a video course Learn object oriented PHP
</div>
You can also change the field name, default value and other attributes in the same change method.
Example of using the change method on the Schema facade to change the field name, default value and other attributes in Laravel
php
Schema::table('users', function (Blueprint $table) {
$table->string('email', 100)->nullable()->change();
});Then run the migration using the command php artisan migrate.
Important notes:
- The
doctrine/dbalpackage must be installed (composer require doctrine/dbal) to use thechange()method on MySQL, PostgreSQL, and SQL Server. - SQLite does not support altering columns, so the
change()method will not work on SQLite databases. - For better context, generate the migration file using Laravel's naming conventions:
php artisan make:migration change_email_type_in_users_table --table=users.