如何在Laravel中使默认值为空 [英] How to make default value null in laravel

查看:698
本文介绍了如何在Laravel中使默认值为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在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.

推荐答案

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

这篇关于如何在Laravel中使默认值为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆