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?

  • you are already using `nullable()` for image column. use it for those column which you want to be null as default. – zahid hasan emon Oct 24 '19 at 07:03
  • @zahidhasanemon yes When I first create this migration I added nullable(). Now I want it to add to those columns too. – Gayal Seneviratne Oct 24 '19 at 07:13
  • Does this answer your question? [Laravel Migration Change to Make a Column Nullable](https://stackoverflow.com/questions/24419999/laravel-migration-change-to-make-a-column-nullable) – miken32 Nov 19 '20 at 04:42

5 Answers5

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();
        });
    }
}
nmfzone
  • 2,755
  • 1
  • 19
  • 32
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
Mohamed Raza
  • 818
  • 7
  • 24
Lizesh Shakya
  • 2,482
  • 2
  • 18
  • 42
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

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();
    });
}
alprnkeskekoglu
  • 288
  • 1
  • 7
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();