4

This is my vehicles table. I want to change my database structure by using a migration


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

class CreateVehiclesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('vehicles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('image')->nullable();
            $table->string('reg_no')->unique();
            $table->string('fuel_type');
            $table->string('capacity');
            $table->double('rate');
            $table->boolean('req_carrier');
            $table->date('service_date');
            $table->integer('service_freq_km');
            $table->integer('service_freq_months');
            $table->date('insurance_date');
            $table->integer('insurance_freq_months');
            $table->date('eco_date');
            $table->integer('eco_freq_months');
            $table->date('licence_date');
            $table->integer('licence_freq_months');
            $table->integer('current_company');
            $table->string('status')->default("available");
            $table->timestampsTz();

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('vehicles');
    }
}

I want to give nullable values to these columns. 1.service_date 2.service_freq_km 3.service_freq_months

How can I alter these columns as nullable in mysql?

3
  • you are already using nullable() for image column. use it for those column which you want to be null as default. Commented Oct 24, 2019 at 7:03
  • @zahidhasanemon yes When I first create this migration I added nullable(). Now I want it to add to those columns too. Commented Oct 24, 2019 at 7:13
  • Does this answer your question? Laravel Migration Change to Make a Column Nullable Commented Nov 19, 2020 at 4:42

5 Answers 5

5

You can read the docs about Modifying Columns.

If you want these feature, you need to install this package first:

composer require doctrine/dbal

Then, you need to create another migration, for example:

2019_10_24_xxxxxx_change_columns_to_nullable_in_vehicles.php

<?php

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

class ChangeColumnsToNullableInVehicles extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('vehicles', function (Blueprint $table) {
            $table->date('service_date')->nullable()->change();
            $table->integer('service_freq_km')->nullable()->change();
            $table->integer('service_freq_months')->nullable()->change();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('vehicles', function (Blueprint $table) {
            $table->date('service_date')->nullable(false)->change();
            $table->integer('service_freq_km')->nullable(false)->change();
            $table->integer('service_freq_months')->nullable(false)->change();
        });
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

install the package in order to update the tables composer require doctrine/dbal

Since you have migrated the migration files, you now need to create a new migration file using artisan command:

php artisan make:migration change_nullable_field_columns_to_vehicles_tables --table=vehicles

In newly created migration file, add the following codes

public function up() {
        Schema::table('vehicles', function (Blueprint $table) {
            $table->date('service_date')->nullable()->change();
            $table->integer('service_freq_km')->nullable()->change();
            $table->integer('service_freq_months')->nullable()->change();
        });
    }

//For php artisan down

public function down(){
            Schema::table('vehicles', function (Blueprint $table) {
                $table->date('service_date')->nullable(false)->change();
                $table->integer('service_freq_km')->nullable(false)->change();
                $table->integer('service_freq_months')->nullable(false)->change();
            });
}

Now you can execute migrate command

php artisan migrate

1 Comment

composer require doctrine/dbal installation of this package is required to complete your answer
0

You need to create new migration file that is name "2019_10_24_00000_update_vehicle_tables"

if(Schema::hasTable('vehicles')) {
    Schema::table('vehicles', function($table)
    {
        $table->date('service_date')->nullable();
        $table->integer('service_freq_km')->nullable();
        $table->integer('service_freq_months')->nullable();
    });
}

2 Comments

@alprnkesskekoglu should I write this inside the "up" function?
i forget it sorry... you need write inside the "up" function
0

create new migration class for alter table and use this up function

public function up()
{
    Schema::table('vehicles', function (Blueprint $table) {
        $table->date('service_date')->nullable();
        $table->integer('service_freq_km')->nullable();
        $table->integer('service_freq_months')->nullable();

   });
}

Schema::table use for alter table and Schema::create is use for create new table

2 Comments

@Rasshid I have already migrated the migration. Now I want to change it as $table->date('service_date')->nullable(); how can I alter the table?
to update a table composer require doctrine/dbal this package is required
0
$table->string('first_name')->default('DEFAULT');

If the default value is supposed to be null, make it nullable instead.

$table->string('name')->nullable();

$table->string('name')->nullable()->default('NULL');

$table->string('name')->nullable()->default(NULL);

$table->string('name')->nullable()->default();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.