user3413723 user3413723 - 1 year ago 257
PHP Question

Update Created_at timestamp Laravel db seeding

I'm running an app that processes user data entered over a period of time. For that, I need to change the timestamps in the data I use to seed the database. I have a nice loop to do it, but the problem is, when I run the database seeding, nothing happens. I'm wondering if there is a Laravel specific thing that prevents this, and if so how to get around it.

$record->created_at = $fakeCreateDate->format('Y-m-d H:i:s');

Answer Source

Yes, created_at and updated_at are "special". Laravel sets the timestamp for created_at at create time and updated_at at update time.

You can turn them off by doing this inside your model

public $timestamps = false;

By default, Eloquent will maintain the created_at and updated_at columns on your database table automatically. Simply add these timestamp columns to your table and Eloquent will take care of the rest. If you do not wish for Eloquent to maintain these columns, add the following property to your model

Once a model is defined, you are ready to start retrieving and creating records in your table. Note that you will need to place updated_at and created_at columns on your table by default. If you do not wish to have these columns automatically maintained, set the $timestamps property on your model to false.