D
D
Dmitry2019-05-03 10:16:49
Laravel
Dmitry, 2019-05-03 10:16:49

How to correctly add date when migrating to Laravel?

Hello, I created a migration. In it I create the table and I write down the initial data.

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

class CreateStocksTable extends Migration
{
    public function up()
    {
        Schema::create('stocks', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->charset = 'utf8';
            $table->collation = 'utf8_general_ci';

            $table->increments('id');
            $table->text('title');
            $table->longText('content');
            $table->timestamps();
        });

        DB::statement('INSERT INTO `stock` (`title`, `content`, `created_at`, `updated_at`) VALUES ('some title', 'some content', '2019-04-29 00:00:00', '2019-04-29 00:00:00')');
    }

    public function down()
    {
        Schema::dropIfExists('stocks');
    }
}

I do not specify the id, the auto-increment will be filled in automatically, if you do not specify the date, then after that you will have to fill in manually.
Is it correct to set the date in the migration as a string, or can it be filled in automatically with the date and time of the migration?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
netrox, 2019-05-03
@ddimonn8080

Seeders are used to record the initial data .
For dates you can use:

Schema::create('stocks', function (Blueprint $table) {
            $table->engine = 'InnoDB';

            $table->increments('id');
            $table->string('title');
            $table->text('content');
            $table->dateTime('date')->default(DB::raw('CURRENT_TIMESTAMP'));
        });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question