➥LARAVEL MIGRATION: UPDATING A COLUMN WITHOUT LOOSING DATA:
Example:
Suppose i have a database table which has this fields:
id
slider_title
slider_description
and the migration of that table looks like this:
Schema::create('slide_cases', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('slider_title')->nullable();
$table->string('slider_description')->nullable();
$table->timestamps();
});
});
Now, if i want to change the slide_description field data type from string to longText using laravel migration, simply do the following steps:
1) Open your command prompt and type:
php artisan make:migration update_slider_description_in_slide_cases --table=slide_cases
➽PLEASE KEEP IN MIND that slide_cases is my table name; so you have to give your table name in that place :)
2) Now, in your newly created migration file for updating slider_description do the followings:
Schema::table('slide_cases', function (Blueprint $table) {
$table->longText('slider_description')->nullable()->change();
});
➽PLEASE KEEP IN MIND that while updating in laravel migration you have to give change() at last :)
3) Finally you are ready to migrate now! ;)
In your command prompt just type:
php artisan migrate
I hope you have finally solved the problem :D Let me know your feedback.
➥LARAVEL MIGRATION: DROP/DELETE A COLUMN WITHOUT LOOSING DATA:
Example:
Suppose i have a database table which has this fields:
id
member_name
member_address
Suppose i have a database table which has this fields:
id
member_name
member_address
and the migration of that table looks like this:
Schema::create('team_members', function (Blueprint $table) {
Schema::create('team_members', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('member_name');
$table->string('member_address');
$table->timestamps();
});
Now, if i want to drop the member_address field data using laravel migration, simply do the following steps:
1) Open your command prompt and type:
php artisan make:migration drop_member_address_in_team_members --table=team_members
➽PLEASE KEEP IN MIND that team_members is my table name; so you have to give your table name in that place :)
2) Now, in your newly created migration file for dropping member_address do the followings:
Schema::table('team_members', function (Blueprint $table) {
$table->dropColumn('designation');
});
➽PLEASE KEEP IN MIND that while dropping column in laravel migration you have to write dropColumn() :)
3) Finally you are ready to migrate now! ;)
In your command prompt just type:
php artisan migrate
I hope you have finally solved the problem :D Let me know your feedback.

Comments
Post a Comment