Laravel schema default current timestamp value example

August 1, 2017 | Category : Laravel PHP

Some days ago, i was working my laravel 5.4 application and i require to create new migration with create_at column only, update_at was no more require. So i decided to set default current timestamp value to created_at column.

But i was thing how is it possible to set default current timestamp to created_at column using laravel schema. i started to read docs and i found useCurrent() of schema builder. So we can simply use as following syntax and example.

Syntax:

$table->timestamp('column_name')->useCurrent();

Example:

<?php


use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;


class CreateProductTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('products', function (Blueprint $table) {

$table->increments('id');

$table->string('name');

$table->string('slug');

$table->text('details');

$table->timestamp('created_at')->useCurrent();

});

}


/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::drop("products");

}

}

As above example, you can see how is it possible.

I hope you found your best solution...