Search code examples
phplaraveleloquentmigrationblueprint

Laravel, MariaDB timestamp column without default and onupdate trigger


I have following migration:

Schema::create('auctions', function (Blueprint $table) {
    $table->id();
    $table->timestamp('expired_at');
    $table->timestamp('sold_at')->nullable()->default(null);
    // other fields
});

Which creates a table with this structure: enter image description here

Every time I try to update sold_at field only, It changes the expire_at field also, which is so annoying and against my project's logic! How should I fix my migration to prevent this?

My update record code:

Auction::query()->where('id',1)->update([
    'sold_at' => now()
]);

Solution

  • Change your column to a datetime column:

    Schema::create('auctions', function (Blueprint $table) {
        $table->id();
        $table->dateTime('expired_at');
        $table->timestamp('sold_at')->nullable()->default(null);
        // other fields
    });
    

    https://laravel.com/docs/9.x/migrations#column-method-dateTime