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...
Do you like below Tutorials ?
- Laravel 5.6 - Collection could not be converted to int
- Laravel - How to generate secure https URL from route?
- Laravel - Vue JS File Upload Example
- How to get last 7 days data in Laravel?
- Laravel Validation required if other field empty example
- Laravel Eloquent - When Case Statement in Select Query Example
- Laravel 7.x and 6.x Passing Variable to Javascript Example
- How to pass PHP variables in JavaScript or jQuery?