PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 13, 2022

[FIXED] How to change database column 'null' to 'nullable' using laravel migration in mysql?

 April 13, 2022     alter-table, laravel, migration, mysql     No comments   

Issue

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?


Solution

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();
        });
    }
}


Answered By - nmfzone
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing