Modify Columns using Migration in Laravel
- Posted on
- Laravel
- By Deepak Talwar
Laravel's migration changes lets you undo the latest or a set number of database changes.
This post is focused on laravel migration changes

Changing Column Datatype
php artisan make:migration change_datatype_in_product_backups_table
public function up(): void
{
Schema::table('product', function (Blueprint $table) {
$table->string('desc')->nullable()->change();
});
}
public function down(): void
{
Schema::table('product', function (Blueprint $table) {
$table->boolean('desc')->nullable()->change();
});
}
Rename Existing Column Name
php artisan make:migration rename_column_in_table
public function up()
{
Schema::table('product', function (Blueprint $table) {
$table->renameColumn('desc', 'description');
});
}
public function down()
{
Schema::table('product', function (Blueprint $table) {
$table->renameColumn('description', 'desc');
});
}
Modifying database indexes, including adding, renaming, or removing them
Make column nullable
php artisan make:migration change_col_to_nullable_in_prodcuct_table --table=product
public function up(){
Schema::table('product', function (Blueprint $table) {
$table->text('price')->nullable()->change();
});
}public function down(){
Schema::table('product', function (Blueprint $table) {
$table->text('price')->nullable(false)->change();
});
}
Make column non nullabl
public function up(){
Schema::table('product', function (Blueprint $table) {
$table->type('price')->nullable(false)->change();
});
}public function down(){
Schema::table('product', function ($table) {
$table->type('price')->nullable()->change();
});
}In laravel 8, you just have to put this: "->nullable()"
$table->string('name')->nullable();
Change Column Length
public function up() {
Schema::table(‘product’, function (Blueprint $table) {
$table->string(‘name’, 80)->change();
});
}
d