Laravel timestamps() doesn't create CURRENT_TIMESTAMP

51,673

Solution 1

When you insert data not using Eloquent you need to insert timestamps on your own.

If you use:

$x = new MyTable();
$x->title = 'My Awesome Title';
$x->save();

you will have timestamp filled correctly (of course you need to create MyTable model first)

EDIT

If you really want it you can change:

$table->timestamps();

into:

$table->timestamp('created_at')->default(\DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(\DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));

And if you create model for this table, you should set

$timestamps = false;

to make sure Eloquent won't try to set them on his way.

EDIT2

There is also one more important issue. In case you mix setting dates in tables from PHP and in other in MySQL you should make sure that both in both PHP and MySQL there's exact same datetime (and timezone) or you should use the same date comparison as you set in record (either MySQL or PHP). Otherwise when running queries you might get unexpected results for example

SELECT * FROM mytable WHERE DATE(created_at) = CURDATE()

might be different than running query with passing PHP date

"SELECT * FROM mytable WHERE DATE(created_at) = '".date('Y-m-d")."'"

because on PHP server it might be for example 2015-12-29 but on MySQL server 2015-12-30

Solution 2

For later versions, you can simply use. (source)

$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrent();

Solution 3

i would use the carbon library if i am seeding in the timestamps and set that up in the factory. if not you could do something like this :

$timestamps = false;

and i would remove the the $table->timestamps(); from the migration if i am not going to use it.

Share:
51,673

Related videos on Youtube

Get Off My Lawn
Author by

Get Off My Lawn

Currently working on a node web framework Red5 Http Server, which is still just beta, but feel free to check it out! Any feedback would be great! You can checkout the code on Github, this includes the website, installer and framework. Thanks for checking it out!

Updated on October 25, 2020

Comments

  • Get Off My Lawn
    Get Off My Lawn over 3 years

    I have a migration that has the timestamps() method, and then I have a seed to seed this table.

    Schema::create('mytable', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->timestamps();
    });
    

    The seed looks like this:

    DB::table('mytable')->insert([
        [
            'title' => 'My Awesome Title'
        ]
    ]);
    

    When it all gets run using:

    php artisan migrate:refresh --seed
    

    The item gets inserted, but the values of created_at and updated_at are both 0000-00-00 00:00:00 why are they not set correctly?

    here are the column schemes that it creates:

    `created_at` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
    `updated_at` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
    

    I would like these schemes:

    `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    
    • Jeff Lambert
      Jeff Lambert over 8 years
      Use Eloquent models to create records instead of the Fluent interface and they should be set correctly.
  • Get Off My Lawn
    Get Off My Lawn over 8 years
    Okay I do have that (just commented out). So that is how it has to be done, I thought that laravel would have done that. Okay thanks
  • Marcin Nabiałek
    Marcin Nabiałek over 8 years
    @GetOffMyLawn I've added one more info in EDIT2
  • Raiika
    Raiika about 6 years
    you don't need to specify $timestamps = false; with this
  • bmatovu
    bmatovu about 6 years
    No, you don't because your still have the timestamps but with just different default values