Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Chill's avatar
Level 7

Make Laravel (and Livewire) think we're in the past

I'm working very heavily with dates and while developing, would like to have the app think we're in a past date so that I can use the app and see what it would look like. Mind you that I know how to do this on my unit tests and that works just fine, but I'd like to be able to actually use the app normally.

Things I've tried:

Adding a middleware with something like this:

$knownDate = Carbon::create(config('app.fixed_date'));
Carbon::setTestNow($knownDate);

This kind of works but breaks anything that has a form because the CSRF token is validated against the real date, not the one provided by Carbon:now(). This also breaks Livewire for the same reason.

So I tried completely removing the CSRF middleware and any mentions to @csrf but for some reason Laravel will not let me log in anymore (I'm using a stock Jetstream starting scaffolding with Laravel 10). This also seems like a pain in the butt to do every time I wish to test in this way.

I suspect the key here is to force Laravel's CSRF token generation to use my fake date but I can't figure it out. Any ideas on this or any other potential solutions would be very welcome!

0 likes
11 replies
jlrdw's avatar

I don't understand what you are trying to accomplish, can you give a better example?

Chill's avatar
Level 7

Oh, let me try again.

Imagine you have a Blade view that lists a collection of music events. The list shows the past events in red and the future events in green. I want to fake the app's date so that I can reload the page and see some events in red and some events in green. That's what I mean by faking the date; making Laravel think that today's date and time (aka what Carbon::now() returns) is a different date than what it currently is.

Obviously this is a massively (massively) simplified version of what I need to do, but if I can achieve this, then I'm good.

jlrdw's avatar

Just have some events and use some past dates.

Chill's avatar
Level 7

Would you care to elaborate what you mean by "have some events"?

Chill's avatar
Level 7

@jlrdw As I said, I have a very complex object graph so that's not practical in my case. If I "just have some events and use some past dates" I would have to create and recreate the data every single time I want to test in this way. Possible? Yes. Also, massively impractical. If I can fake an arbitrary now() date, I only have to copy the production data over to my local system and see exactly what would happen on any arbitrary date.

Note: The music events above are just a simplified example (my app has nothing to do with music events).

webrobert's avatar

@Chill, the less vague you are here the more help you can get.

I would have to create and recreate the data every single time I want to test in this way. Possible?

Yes, This is generally how testing works. The test creates the data. To perform the test then tears it back down. again.

Chill's avatar
Level 7

@webrobert To be clear, I'm trying to get a generic way to solve this, not a specific "do this, do that" kind of answer. There may have been some confusion because the word "test" may mean different things. My question is not about unit/integration/etc tests, it's about "using" the app (aka manually testing).

The question that needs answered here is:

Is there a way to make Laravel think it's a different date other than what it currently is?

Perhaps the answer is no, and that's OK.

ZohaibAhmed's avatar

how to enable our terminal on symbol host on laravel project

Please or to participate in this conversation.