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.

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? Commented Nov 23, 2016 at 22:40

6 Answers 6

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();
Sign up to request clarification or add additional context in comments.

4 Comments

it gives me again the same error, i've tried that before
sorry. of course, if the default value should be null, you have to make the column nullable instead.
nothing, again the same error, i've tried default(null), nullable(), nullable()->default(null) and always the same..
you did migrate:rollback and migrate again right ? can we have a look on the error message ?
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

Comments

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!

Comments

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

Comments

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.

Comments

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

empirically...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.