jonm
jonm

Reputation: 131

How to set a table field Not Null in Laravel 5.4 migration

I am using laravel 5.4 migrations.There is nullable() options . But how Can I set a table field as Not null in Laravel 5.4 migrations ?

Upvotes: 1

Views: 2534

Answers (2)

Romnick Susa
Romnick Susa

Reputation: 1288

It is automatically not null if you didn't add nullable() method on your migration.

$table->string('col_test1')->nullable();  
//This can be  null. It will run a mysql statement like this
col_test1 VARCHAR(255)

$table->string('col_test2');         
//This should not be null . It will run a mysql statement like this
col_test2 VARCHAR(255) NOT NULL,

If you want more details just navigate here

Upvotes: 3

julianstark999
julianstark999

Reputation: 3616

A database field can only be null or not null.

So for laravel if you call ->nullable() in the migration you allow the field to be null otherwise it is not null without any specific configuration.

example

// this will be not null
$table->string('col1');
// this can be null
$table->string('col1')->nullable();

Upvotes: 2

Related Questions