Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make default value null in laravel

I'm making register form in laravel, first I create the migration

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('first_name');
            $table->string('last_name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

I want first to register name, email and password without first_name and last_name, but when I click register it gives me error that first_name and last_name don't have default values..so how to make default values null, because I want to update that columns later.

like image 461
Devmasta Avatar asked Nov 23 '16 21:11

Devmasta


2 Answers

$table->string('first_name')->default('DEFAULT');

edit: if the default value is supposed to be null, make it nullable instead.

$table->string('first_name')->nullable();
like image 113
LorenzSchaef Avatar answered Oct 19 '22 11:10

LorenzSchaef


In Laravel 5.8 you can try it in migration script as below;

public function up()
{
    if (Schema::hasTable('table_name')) {
        Schema::table('table_name', function (Blueprint $table) {
            $table->string('column_name')->nullable();
        });
    }
}

Reference: https://laravel.com/docs/5.8/migrations

like image 28
Dasitha Abeysinghe Avatar answered Oct 19 '22 10:10

Dasitha Abeysinghe