1

I create migration like this: php artisan make:migration create_categories_table --create categories

Then I add some columns, like:

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

and then: php artisan migrate

This creates table categories, then I insert into categories:

 insert into categories
 (name)
 values
 ('sport')

This inserts row but created_at column is null instead of current timestamp.

What is my mistake?

I did (mysql command): show create table categories:

CREATE TABLE `categories` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
`name` varchar(150) COLLATE utf8mb4_unicode_ci NOT NULL, 
`created_at` timestamp NULL DEFAULT NULL, 
`updated_at` timestamp NULL DEFAULT NULL, 
 PRIMARY KEY (`id`) ) 
ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4     
COLLATE=utf8mb4_unicode_ci 

which clearly shows that default value for created_at is NULL. Why?

Oto Shavadze
  • 40,603
  • 55
  • 152
  • 236
  • How do you do the insert? – ka_lin Nov 15 '19 at 10:48
  • Using raw sql editor – Oto Shavadze Nov 15 '19 at 10:48
  • 5
    timestamps are filled when you insert with Laravel/eloquent. Timestamps do not have a default value upon creation. Check how to set defaults [here](https://stackoverflow.com/questions/18067614/how-can-i-set-the-default-value-of-a-timestamp-column-to-the-current-timestamp-w) – ka_lin Nov 15 '19 at 10:49
  • you should rollback that migration then run `php artisan migrate --pretend` to see the SQL it generates .. the timestamp fields need to be updated when you insert/update the record – lagbox Nov 15 '19 at 10:49
  • I edited question with `show create table categories`. tis shows that table is created with `created_at` timestamp NULL DEFAULT NULL`. What is reason of this? – Oto Shavadze Nov 15 '19 at 10:55
  • probably because of all the fun issues between different databases and these 2 particular timestamp fields when it comes to on create and on update – lagbox Nov 15 '19 at 10:56
  • 1
    The `created_at` and `updated_at` fields are updated through Eloquent (when using `$table->timestamps()` they are default `NULL`), so you should use an Eloquent Model to insert/update the data if you want to touch those fields. Otherwise you have to specify the columns yourself with the default `CURRENT_TIMESTAMP`. – Qirel Nov 15 '19 at 10:57
  • Please check this [link](https://stackoverflow.com/questions/18067614/how-can-i-set-the-default-value-of-a-timestamp-column-to-the-current-timestamp-w) it might be help – Ritesh Khatri Nov 15 '19 at 10:59

2 Answers2

5

Instead of using,

$table->timestamps();

use,

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

It will set default value of created at column and updated at column. then after it will store current timestamp.

Ritesh Khatri
  • 1,253
  • 13
  • 29
0

You don't need those columns to have a default value, it's by design.

The created_at and updated_at values are automatically managed by Laravel's Eloquent.

Just make sure you don't write manual insert queries, or even use the insert method

https://laravel.com/docs/master/eloquent#timestamps

raveren
  • 17,799
  • 12
  • 70
  • 83