laravel 如何在laravel中将默认值设为null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40774791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make default value null in laravel
提问by Devmasta
I'm making register form in laravel, first I create the migration
我正在 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');
}
}
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.
我想先注册姓名、电子邮件和密码,而没有 first_name 和 last_name,但是当我单击注册时,它给我错误,即 first_name 和 last_name 没有默认值..所以如何使默认值为空,因为我想更新它列稍后。
回答by LorenzSchaef
$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();
回答by Dasitha Abeysinghe
In Laravel 5.8 you can try it in migration script as below;
在 Laravel 5.8 中,您可以在迁移脚本中尝试,如下所示;
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
回答by Максим Шинкаренко
->default('NULL')->nullable(true)
empirically...
凭经验...