Reputation: 563
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.
Upvotes: 8
Views: 27024
Reputation: 2834
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.
Upvotes: 1
Reputation: 266
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();
Upvotes: 2
Reputation: 1179
As per Laravel 8 there are 2 simple solutions.
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
.
$table->string('total')->nullable();
Happy coding!
Upvotes: 2
Reputation: 451
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
Upvotes: 5
Reputation: 1543
$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();
Upvotes: 23