Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rublopweb's avatar

migration file

HI, I used a laravel migration file to create a column with a default value

 $table->unsignedBigInteger('status)->default(0)

Now, i want to change that column to not have a default value, i want to chose the value of the status and the status cannot be null.

How my migration file should look like?

0 likes
1 reply
LaryAI's avatar
Level 58

To change the column to not have a default value and allow you to choose the value of the status, you can use the change method in your migration file. Here's how your migration file should look like:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class UpdateStatusColumnInTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('your_table_name', function (Blueprint $table) {
            $table->unsignedBigInteger('status')->nullable()->change();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('your_table_name', function (Blueprint $table) {
            $table->unsignedBigInteger('status')->default(0)->change();
        });
    }
}

In the up method, we use the change method to modify the column and make it nullable, which means it won't have a default value. This allows you to choose the value of the status when inserting or updating records.

In the down method, we revert the changes by setting the default value back to 0 using the change method.

Make sure to replace 'your_table_name' with the actual name of your table in the Schema::table method.

Remember to run the migration using the php artisan migrate command to apply the changes to your database.

1 like

Please or to participate in this conversation.