Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set default to NULL with laravel migration

Tags:

laravel

I am adding a field to a table in a migration that I wish to allow to be NULL but also I wish for it to default to NULL. What do I place in the default method? I fear that putting "NULL" in will attempt to place a string of NULLin which I obviously don't want. Please help :)

Schema::table('item_categories', function(Blueprint $table) {     $table->integer('parent_item_category_id')->unsigned()->nullable()->default($what_to_put here); }); 
like image 779
James Stott Avatar asked Dec 19 '14 01:12

James Stott


People also ask

How do I change the default value in Laravel migration?

In our case we are going to do second option - create new migration. If we roll back this migration, we need to remove default value from that field using migration and this is how to do it: Schema::table('photos', function (Blueprint $table) { $table->integer('order')->default(NULL)->change(); });

How do I change a column to nullable in Laravel migration?

The code to change the column to nullable is as "nullable()->change()". ->nullable()->change();

How do I set the default time in Laravel?

This is how you do it, I have checked it and it works on my Laravel 4.2. $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP')); Hope this helps.


1 Answers

When you use the nullable() method on a field, that field will default to NULL.

$table->integer('parent_item_category_id')->nullable(); 
like image 60
James Stott Avatar answered Oct 20 '22 05:10

James Stott