How can I modify a migration in Laravel

0 votes

I'm trying to modify a existing migration. Here is my current migration class:

class CreateLogForUserTable extends Migration
{
    public function up()
    {
        Schema::create('log_for_user', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->string('table_name');
            $table->string('error_message');
            $table->unsignedTinyInteger('error_code');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('log_for_user');
    }
}

I've executed the php artisan migrate command once. Now I need to add ->nullable() method to the error_message column. So I edited my migration, something like this:

.
.
    $table->string('error_message')->nullable();
.
.

But when I execute php artisan migrate again, it says:

Nothing to migrate.

How can I apply the new version of the migration?

Sep 28, 2020 in Laravel by kartik
• 37,520 points
4,174 views

1 answer to this question.

0 votes

Hello @kartik,

You should create a new migration using command:

php artisan make:migration update_error_message_in_log_for_user_table

Then, in that created migration class, add this line, using the change method like this:

class UpdateLogForUserTable extends Migration
{
    public function up()
    {
        Schema::table('log_for_user', function (Blueprint $table) {
            $table->string('error_message')->nullable()->change();
        });
    }

    public function down()
    {
        Schema::table('log_for_user', function (Blueprint $table) {
            $table->string('error_message')->change();
        });
    }
}

To make these changes and run the migration, use the command:

php artisan migrate

and to rollback the changes, use the command:

php artisan migrate:rollback

Hope it helps!!

ThanK you!!

answered Sep 28, 2020 by Niroj
• 82,880 points

Hello.

when exactly should I use change() method? If I want to add an index (like unique()) instead of nullable(), still do I need to use change() like this? $table->string('error_message')->unique()->change();

Yes, change method tells laravel to modify that particular query. As according to laravel perquisite install this package - composer require doctrine/dbal

Hope it helps!!

Thank you!!

Related Questions In Laravel

0 votes
1 answer

How can I build a condition based query in Laravel?

Hello @kartik, Try this: $query = DB::table('node'); if ($published == ...READ MORE

answered Sep 28, 2020 in Laravel by Niroj
• 82,880 points
663 views
0 votes
1 answer

How can I create a migration to add a value to an enum in eloquent?

Hello @kartik, Try using this code: public function up() ...READ MORE

answered Nov 12, 2020 in Laravel by Niroj
• 82,880 points
2,516 views
0 votes
1 answer

How can I find the current language in a Laravel view?

Hello @kartik, The cleanest way to know the ...READ MORE

answered Dec 3, 2020 in Laravel by Niroj
• 82,880 points
4,480 views
0 votes
1 answer

How can I obtain a list of all files in a public folder in laravel?

You could create another disk for Storage ...READ MORE

answered Dec 8, 2020 in Laravel by Niroj
• 82,880 points
3,953 views
0 votes
1 answer

Uncaught Error: Bootstrap's JavaScript requires jQuery

Hello @kartik, You have provided wrong order for ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,880 points
23,237 views
0 votes
1 answer

How to make Bootstrap popover Appear/Disappear on hover instead of click?

Hello @kartik, Set the trigger option of the popover to hover instead ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,880 points
3,280 views
0 votes
1 answer

How to enable Bootstrap tooltip on disabled button?

Hii @kartik, You can wrap the disabled button ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,880 points
5,074 views
+1 vote
2 answers

How to set cache false for getJSON in jQuery?

You can't pass any configuration parameters to ...READ MORE

answered Oct 7, 2020 in JQuery by Amit
• 140 points
2,891 views
+1 vote
1 answer

How can I run specific migration in laravel?

Hello @kartik, Use this command it worked for ...READ MORE

answered Aug 4, 2020 in Laravel by Niroj
• 82,880 points
23,051 views
0 votes
1 answer

How can I create a unique random string in laravel?

Hello @kartik, You can use : sha1(time()) Explanation: sha1 is ...READ MORE

answered Sep 25, 2020 in Laravel by Niroj
• 82,880 points
3,626 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP