How to Change Column Name and Data Type in Laravel?
Teams applying “How to Change Column Name and Data Type in Laravel?” in a production project can use future of work to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hi dev
Column name migration in Laravel is the main topic of this article. You may learn how to modify a column's type in a Laravel migration by using this example. How to rename a column's name in a Laravel migration is shown step by step. I simply described how to modify the data type during migration in Laravel. The Laravel migration can have its column names changed by following the steps below.
I'll give you two examples of data type changes and column renaming using migration in applications built with Laravel 6, Laravel 7, Laravel 8, and Laravel 9.
Install Composer Package:
composer require doctrine/dbal
After successfully install composer package we can change data type and change column name using migration.
Let's see bellow example:
Migration for main table:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->text('body');
$table->boolean('is_publish')->default(0);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
Change Data Type using Migration: body column text data type to long text data type here
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangePostsTableColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->longText('body')->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
}
Rename using Migration: rename title column to name changed.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class ChangePostsTableColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->renameColumn('title', 'name');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
}
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial