ciccioassenza
ciccioassenza

Reputation: 233

retrive records where updated_at is not older than X seconds in Laravel (eloquent)

Following the examples found here on stack overflow I'm trying to do this:

$users = User::where('updated_at', '>', time() - (5))->get();

As the title of the post, I would like to receive only the records where the field "updated_at" is not older than 5 seconds. I also tried something like that:

...where('updated_at', '<', DB::raw('(NOW(), INTERVAL 5 SECOND)'))...

or

...where('updated_at', '>=', time() - (1*1*5))->get();

but keeps giving me even the obsolete records...

Upvotes: 2

Views: 5431

Answers (1)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87789

This way:

$users = User::where('updated_at', '>', \Carbon\Carbon::now()->subSeconds(5)->toDateTimeString())->get();

Upvotes: 11

Related Questions