Laravel Migration with Default Current Timestamp Tutorial

Share this Article
Reading Time: 3 minutes
217 Views

Inside this article we will see the concept i.e Laravel Migration with Default Current Timestamp Tutorial. Article contains the classified information about Set Default Value Of Timestamp In Laravel Migration.

If you are looking to find a solution for Laravel migration where you can set the default current timestamp value then you are at the right place to learn and implement.

Laravel migration provides a useCurrent() and default CURRENT_TIMESTAMP where you can set the default value current timestamps of that column.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

$ composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Example #1: Using useCurrent() For Default Timestamp

Let’s create a migration file.

$ php artisan make:migration create_items_table

It will create a file i.e xxx_create_items_table.php inside /database/migrations folder.

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('items', function (Blueprint $table) {
            $table->id();
            $table->string('title')->nullable();
            $table->text('body');
            $table->boolean('is_active');
            $table->timestamp('creation_date')->useCurrent();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('items');
    }
};

Concept

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

Next, run this command.

$ php artisan migrate

Once you migrate, It will add CURRENT_TIMESTAMP default flag with your creation_date column.

Example #2: Using CURRENT_TIMESTAMP For Default Timestamp

Let’s consider the same migration file.

$ php artisan make:migration create_items_table

It will create a file i.e xxx_create_items_table.php inside /database/migrations folder.

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('items', function (Blueprint $table) {
            $table->id();
            $table->string('title')->nullable();
            $table->text('body');
            $table->boolean('is_active');
            $table->timestamp('creation_date')->default(DB::raw('CURRENT_TIMESTAMP'));;
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('items');
    }
};

Next, run this command.

$ php artisan migrate

Once you migrate, It will add CURRENT_TIMESTAMP default flag with your creation_date column.

We hope this article helped you to learn about Laravel Migration with Default Current Timestamp Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

Get PHP Web Development Courses on Offer in just ₹299
Get PHP Web Development Courses on Offer in just ₹299