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.
Read More: Find NameServers of Domain Using PHP Shell Command
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();
Read More: Laravel How To Pass Multiple Parameters into Route Tutorial
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.
Read More: How To Add Custom Attribute in Laravel Model Tutorial
We hope this article helped you to learn about Laravel Migration with Default Current Timestamp Tutorial in a very detailed way.
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.