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:
- Laravel: Blade Switch Case Statement Example
- Laravel: Switch Case Statement In Controller Example
- How To Use CASE Statement In Laravel
- Laravel: Change Column Name In Migration
- How To Use Where Date Between In Laravel
- How To Add Laravel Next Prev Pagination
- Laravel Remove Column From Table In Migration
- Laravel: Get Month Name From Date
- Laravel: Increase Quantity If Product Already Exists In Cart
- How To Update Pivot Table In Laravel
- How To Dynamic iFrame URL In Elementor
- How To Handle Failed Jobs In Laravel
- How To Remove WooCommerce Data After Uninstall
- How To Get Latest Records In Laravel
- How To Break Nested Loops In PHP Or Laravel
- How To Pass Laravel URL Parameter
- Laravel Run Specific Migration
- 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!