Laravel 10 Change Column Name And Type In Migration

Websolutionstuff | Apr-19-2023 | Categories : Laravel MySQL

In this article, we will see laravel 10 change column names and data types in migration. Here, we will learn about migration column rename. Sometimes we need to change column name and data type after creating of migration so at that time this article is useful.

To rename a column, you may use the renameColumn method provided by the schema builder.

So, let's see how to change column name in laravel 10 migration, laravel 10 migration change column type, and how to rename existing column name in laravel 10.

If you are running a database installation older than one of the following releases, you should ensure that you have installed the doctrine/dbal library via the Composer package manager before renaming a column:

  • MySQL < 8.0.3
  • MariaDB < 10.5.2
  • SQLite < 3.25.0
Example:

You can rename the column names using the renameColumn() function in laravel 10.

Schema::table('users', function (Blueprint $table) {
    $table->renameColumn('from', 'to');
});

 

Install Composer Package:

Now, we will install doctrine/dbal package using the following composer command.

composer require doctrine/dbal

Example:

<?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('description');
            $table->boolean('is_publish')->default(0);
            $table->timestamps();
        });
    }
  
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

 

Change Data Type using Migration:

Example:

<?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('description')->change();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
          
    }
}

 

Rename Column using Migration:

Example:

<?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()
    {
        
    }
}

 


You might also like:

Recommended Post
Featured Post
Laravel 8 Autocomplete Search from Database
Laravel 8 Autocomplete Search...

In this article, we will see the laravel 8 autocomplete search from the database. Using ajax autocomplete...

Read More

Mar-01-2021

Laravel 10 Apexcharts Bar Chart Example
Laravel 10 Apexcharts Bar Char...

In this article, we will see the laravel 10 apexcharts bar chart example. Here, we will learn about how to create a bar...

Read More

May-24-2023

How To Check Email Already Exist Or Not In Laravel
How To Check Email Already Exi...

In this article, we will show how to check whether the email already exists or not in laravel. Also, we will check...

Read More

Aug-07-2020

How To Live Search In Laravel 9 Using Meilisearch
How To Live Search In Laravel...

In this article, we will see how to live search in laravel 9 using Meilisearch. Here we will learn live search in l...

Read More

Dec-13-2022