How to make default value null in laravel
Asked Answered
C

6

8

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.

Coughlin answered 23/11, 2016 at 21:57 Comment(1)
are you rolling back and running migration again or is a new migration to change the values? also which sql version do you have?Chitterlings
V
23
$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();
Velarium answered 23/11, 2016 at 21:58 Comment(4)
it gives me again the same error, i've tried that beforeCoughlin
sorry. of course, if the default value should be null, you have to make the column nullable instead.Velarium
nothing, again the same error, i've tried default(null), nullable(), nullable()->default(null) and always the same..Coughlin
you did migrate:rollback and migrate again right ? can we have a look on the error message ?Gynoecium
V
5

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

Velarize answered 25/8, 2019 at 13:58 Comment(0)
C
2

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!

Cubital answered 1/12, 2020 at 14:1 Comment(0)
A
2

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();
Audiphone answered 6/1, 2021 at 5:55 Comment(0)
D
1

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.

Deucalion answered 2/6, 2021 at 3:32 Comment(0)
N
0
->default('NULL')->nullable(true)

empirically...

Nabob answered 23/2, 2020 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.