I'm using TDD on a project as I work through some Undo functionality. I only want the user to be able to undo the latest action that they have taken, so I'm doing a check to make sure that the id of the action being undone is the most recent database entry. Something like
$undoAction->id == Action::latest()->first()->id
When I run it on real-world data, it works great, but I'm having trouble during testing. Since phpunit creates all of the actions in miliseconds (via factories), they all have the same created_at value, and the query is actually returning the first action, not the last.
I was able to make it work with
Action::latest('created_at')->latest('id')->first() which I assume is sorting by created_at and then id, but it feels gross.
Is there a better way to get the latest Action row when they all have the same created_at value?