Home » Set Default Value Of Timestamp In Laravel Migration

Set Default Value Of Timestamp In Laravel Migration

Last updated on June 13, 2021 by

Sometimes, while creating migration files we might need to set the default value of the custom timestamp in Laravel migration. In this article, we will see how to set the default value of the timestamp in Laravel migration with very little effort. Let’s just do it.

Set Default Value Of Timestamp In Laravel Migration

Let’s suppose, you want to customize the users migration in Laravel and wanted to add registered_at column. Which will capture the time & date of registration. But to do that would you write some codes to capture that?

We don’t think so to do that you need to write any extra line of code. You can do that by simply set the default value of that timestamp in Laravel migration using useCurrent() function and you’re done.

Let’s customize the users migration table:

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name')->nullable();
            $table->string('username')->unique();
            $table->string('avatar')->unique()->nullable();
            $table->string('email')->unique()->nullable();
            $table->enum('user_role',['admin', 'user'])->default('user');
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->string('otp')->nullable();
            $table->rememberToken();
            $table->timestamp('registered_at')->useCurrent();
            $table->timestamps();
        });
    }

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

Another Way:

You can also use the Raw string expression of DB facade.

$table->timestamp('created_at')->default('CURRENT_TIMESTAMP');

Notes: You will need to have the MySQL version >= 5.6.5 to have multiple columns with CURRENT_TIMESTAMP

Extra Perk:

We have researched on Google and we have found something very interesting and that is.

We find it more useful to have null in the updated_at column when the record is been created but has never been modified. It reduces the DB size just a little) and it’s possible to see at the first sight that the data has never been modified.

To do that add following code in migration file.

$table->timestamp('updated_at')
      ->default(DB::raw('NULL ON UPDATE CURRENT_TIMESTAMP'))->nullable();

Additionally, read our guide:

  1. Laravel: Blade Switch Case Statement Example
  2. Laravel: Switch Case Statement In Controller Example
  3. How To Use CASE Statement In Laravel
  4. Laravel: Change Column Name In Migration
  5. How To Use Where Date Between In Laravel
  6. How To Add Laravel Next Prev Pagination
  7. Laravel Remove Column From Table In Migration
  8. Laravel: Get Month Name From Date
  9. Laravel: Increase Quantity If Product Already Exists In Cart
  10. How To Update Pivot Table In Laravel
  11. How To Dynamic iFrame URL In Elementor
  12. How To Handle Failed Jobs In Laravel
  13. How To Remove WooCommerce Data After Uninstall
  14. How To Get Latest Records In Laravel
  15. How To Break Nested Loops In PHP Or Laravel
  16. How To Pass Laravel URL Parameter
  17. Laravel Run Specific Migration
  18. How To Fix Elementor Icons Not Showing

That’s it from our end. We hope this article helped you to learn how to set the default value of the timestamp in Laravel migrations.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!

 
 

Leave a Comment