skip to Main Content

I have a migration with $table->datetime('published_at');
and it is already migrated and data is filled in. Now, I have to make this column ->nullable(), but without using migrate:refresh or rollback. How can I achieve this or is it even possible.

Note: No raw sql queries or phpmyadmin. Looking for migration methods.

3

Answers


  1. create new migration and add code below:

    Schema::table('table_name', function (Blueprint $table) {
        $table->string('published_at')->nullable()->change();
    });
    

    or if using mysql:

    You can change your table’s structure directly in phpmyadmin

    Go to phpmyadmin -> table -> structure

    edit publishd_at column and tick null

    Login or Signup to reply.
  2. Simple way is to just run a query like this:

    ALTER TABLE my_table MODIFY published_at DATETIME NULL
    
    Login or Signup to reply.
  3. You can create a new migration:

    php artisan make:migration change_published_at_to_nullable
    

    This will generate a new Migration file called

    XXXX_YYY_ZZZ_000000_change_published_at_to_nullable.php
    

    In this migration file, add the following code:

    public function up(){
      Schema::table("table", function (Blueprint $table) {
        $table->string("published_at")->nullable()->change();
      });
    }
    
    public function down(){
      Schema::table("table", function (Blueprint $table) {
        $table->string("published_at")->nullable(false)->change();
      });
    }
    

    When the command php artisan migrate is run, the published_at column will be changed to allow null. If you need to reverse this, php artisan migrate:refresh, or php artisan migrate:reset, or php artisan migrate:rollback --step will change the column back to allow not null.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search