Devmasta
Devmasta

Reputation: 563

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.

Upvotes: 8

Views: 27024

Answers (6)

Hedayatullah Sarwary
Hedayatullah Sarwary

Reputation: 2834

You can make first_name & last_name as nullable:

<?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')->nullable();
            $table->string('last_name')->nullable();
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

When you use the nullable() method on a field, that field will default to NULL.

Upvotes: 1

Menisha Myelwaganam
Menisha Myelwaganam

Reputation: 266

I'm making register form in laravel, first I create the migration If you want a default value instead of NULL you can do as follow in migration schema:

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

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

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

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

Upvotes: 2

Marinario Agalliu
Marinario Agalliu

Reputation: 1179

As per Laravel 8 there are 2 simple solutions.

  1. If you want a default value instead of NULL you can do as follow in migration schema:

    $table->string('total')->default('0');

This means that the default value will be 0.

  1. If you want a NULL value you can do as follow in your schema: $table->string('total')->nullable();

Happy coding!

Upvotes: 2

->default('NULL')->nullable(true)

empirically...

Upvotes: 0

Dasitha Abeysinghe
Dasitha Abeysinghe

Reputation: 451

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

Upvotes: 5

LorenzSchaef
LorenzSchaef

Reputation: 1543

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

Upvotes: 23

Related Questions