At some level of project creation, you might want to change the column type in Laravel migration. In this, article we will see the steps to change the column type in Laravel migration. Let’s just dive into it.
Change Column Type In Laravel Migration
The Laravel provides the change()
method to change the column type and attributes. Let’s take a simple example.
Let’s create a new migration for users
table and then we will change any column type and attributes into it for better understanding.
php artisan make:migration create_users_table
The above command will create a new migration in database/migrations
directory. We have added the following code into the newly created migration.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->uuid('uuid')->unique();
$table->text('title')->nullable();
$table->text('first_name', 10);
$table->text('last_name');
$table->string('email', 20)->unique();
$table->string('password')->nullable();
$table->text('gender')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Now, let’s migrate the database using php artisan migrate
command and the users
table will be created into our database.
Let’s suppose, now we want to change the first_name
column type from text
to string
and increase the size from 10
to 50
. To do that we need to follow the following steps:
- First and most important thing is to check whether or not your project has doctrine/dbal library, if not then please install it first by running the following command:
composer require doctrine/dbal
- Create new migration file to update the column
php artisan make:migration update_users_table --table="users"
A new migration file will be created into the database/migrations
directory.
- Let’s now change the type of
first_name
columnup()
method and don’t forget to add the reverse effect in thedown()
method.
Theup()
method will be executed when we run thephp artisan migrate
command and thedown()
method will be executed when we run thephp artisan migrate:rollback
command. Thus, you can balance between the migrate and rollback command.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class UpdateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('first_name', 50)->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->text('first_name', 10)->change();
});
}
}
- Finally, run the migrate command to update the table.
php artisan migrate
Additionally, read our guide:
- Base Table Or View Already Exists In Laravel Migration
- Add Column After A Column In Laravel Migration
- Specified Key Was Too Long Error In Laravel
- AJAX PHP Post Request With Example
- How To Use The Laravel Soft Delete
- How To Add Laravel Next Prev Pagination
- Laravel Remove Column From Table In Migration
- Difference Between Factory And Seeders In Laravel
- Laravel: Increase Quantity If Product Already Exists In Cart
- How To Calculate Age From Birthdate
- How To Check Laravel PHP Version
- How To Handle Failed Jobs In Laravel
- How To Remove WooCommerce Data After Uninstall
- How To Get Latest Records In Laravel
- How To Break Nested Loops In PHP Or Laravel
- How To Pass Laravel URL Parameter
- Laravel Run Specific Migration
That’s it from our end. We hope this article helped you to learn how to change column names and rename foreign key columns in Laravel migration.
Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!