Andrey
Andrey

Reputation: 15

Filter query column with time() Laravel

I have read a lot of posts in the forum but haven't been able to understand this error.

I need to count user's sessions ( login times?) with the timestamp less than 3 days.

My query.

Session::where('user_id', $user->id)
                ->whereRaw('`timestamp` < ' . time() -3 * 24 * 60 * 60)
                ->orWhereRaw('timestamp' . ' IS NULL')
                ->orderBy('date')->limit($usc - $user->devices)->delete();

Here is my mistake.

ReflectionException

Class App\UseCases\UserService does not exist

If I remove ->whereRaw('`timestamp` < ' . time() -3 * 24 * 60 * 60) from query, the mistake disappears.

I don't seem to find the error.

Thank you in advance.

Upvotes: 0

Views: 368

Answers (1)

MaartenDev
MaartenDev

Reputation: 5802

You can use the whereDate filter provided by laravel:

Session::where('user_id', $user->id)
                ->whereDate('timestamp', '<', date('Y-m-d', time() -3 * 24 * 60 * 60));
                ->orWhereRaw('timestamp' . ' IS NULL')
                ->orderBy('date')->limit($usc - $user->devices)->delete();

Docs: https://laravel.com/docs/7.x/queries#where-clauses

Upvotes: 1

Related Questions