I have this eloquent:
$users = [1,2,3,4,5];
$transactions = Transaction::whereIn('user_id', $users)
->where(DB::raw('DATE(MAX(created_at))'), Carbon::now()->subMonth()->format('Y-m-d'))->get();
return $transactions;
But it return me this error: General error: 1111 Invalid use of group function (SQL: select * from transactions where 0 = 1
How can I get all the users that last transaction equal to past 30 days?
Here is the raw query incase you needed:
select * from `transactions` where `0` = 1 and DATE(MAX(created_at)) = ?
Table Structure:
Users Table: id (not autoincrement), name, gender, created_at
Transactions Table: id (not autoincrement), user_id (FK), amount, created_at
My current solution:
Iterate every user and check their max created_at
.
Thanks