How to Change Column Length using Laravel Migration?
This simple article demonstrates of change column length laravel migration. We will use laravel migration change column length. Here you will learn laravel change column length migration. I’m going to show you about migration change column length in laravel.
we are managing proper length of data type then it's very help full for db storage. we can save space. but if you added length like 50 with string data type and latter you need to upgrade it then how you will do. here is a simple example how to update column length in laravel migration.
let's see bellow example:
Migration:
<?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', 50);
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
Change Column Length using Migration
now, we need to update title string 50 length to 100. we can do it with following migration:
Install Composer Package:
composer require doctrine/dbal
Migration:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UpdatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->string('title', 100)->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
}
now you can run it.
i hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Rollback Migration in Laravel?
- Laravel Migration Add Comment to Column Example
- How to add Default Value of Column in Laravel Migration?
- Laravel Migration Custom Index Name Example
- Laravel Migration Custom Foreign Key Name Example
- How to Add Index in Laravel Migration?
- How to Add Foreign Key in Laravel Migration?
- Laravel Migration - How to Add New Column in Existing Table ?
- How to Change Table Name using Laravel Migration?
- How to Remove Column from Table in Laravel Migration?
- How to Change Column Name and Data Type in Laravel Migration?
- How to Drop Foreign Key Constraint in Laravel Migration?