I'm using Laravel Framework version 5.2.7. I created a database migration as shown below:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateLessonsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('lessons', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('body');
$table->boolean('some_bool');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('lessons');
}
}
I went in the virtual machine's MySql database and used the command desc lessons;
, this is what I'm getting:
I don't understand why do created_at
and updated_at
columns have NULL
as the Default
. I went to Laravel 5.2 documentation and saw this:
$table->timestamps(); Adds created_at and updated_at columns.
$table->nullableTimestamps(); Same as timestamps(), except allows NULLs.
So according to the documentation $table->timestamps()
should not allow NULL
as default. But still I'm getting created_at
and updated_at
columns having NULL
as the default. Why is this?
In fact these columns should be like this table:
If you use $table->timestamp('created_at')
instead of $table-timestamps()
in your schema builder, default value will be CURRENT_TIMESTAMP
and it won't be NULL
.
Likewise to store update time of a record:
$table->timestamp('updated_at')
.
The difference is in just a letter 's' and have to mention the column name such as created_at
/created
, updated_at
/updated
.