如何在laravel中使默认值为null

Dev*_*sta 2 laravel laravel-5

我正在laravel中创建注册表单,首先我创建了迁移

<?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');
    }
}
Run Code Online (Sandbox Code Playgroud)

我想首先注册没有first_name和last_name的名称,电子邮件和密码,但是当我单击注册时,它给出了我的错误,即first_name和last_name没有默认值..所以如何使默认值为null,因为我想更新那个列后来.

Lor*_*aef 8

$table->string('first_name')->default('DEFAULT');
Run Code Online (Sandbox Code Playgroud)

编辑:如果默认值应为null,则使其可为空.

$table->string('first_name')->nullable();
Run Code Online (Sandbox Code Playgroud)


Das*_*ghe 5

在 Laravel 5.8 中,您可以在迁移脚本中尝试,如下所示;

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

参考:https : //laravel.com/docs/5.8/migrations