Summer Sale! All accounts are 50% off this week.

Sinnbeck's avatar
Level 102

Overwrite created_at in tests with factories

I am writing some tests and need to seed some entries in the database that is a few days old. Sadly this seems to be hard for some reason. I want to overwrite the created_at timestamp but I cannot seem to find a way of doing so with factories.

I have tried

  1. Using Carbon::setTestNow();. But that wont work as laravel uses Date::now() for timestamps.
  2. Adding ['created_at' => now()->subDays(2)] to the factory, which also does not work.

Any ideas?

0 likes
2 replies
Tray2's avatar

Can you somehow disable timestamps for the model when running a test?

1 like
Sinnbeck's avatar
Sinnbeck
OP
Best Answer
Level 102

Ah! Seems it works with the built in timetravel og laravel

$this->travelTo(now()->subDays(2));
2 likes

Please or to participate in this conversation.