I'm trying to pulling a group of users who have not made a transaction since a specified date.
This is my query:
$notActive = DB::table('transactions')
->join('users','transaction.user_id', '=', 'users.user_id' )
->select(DB::raw('users.user_id, users.name, max(transaction.last_posted_date) AS lastDate'))
->groupBy('users.user_id')
->where('lastDate', '<', ''.$not.'');
return Datatables::of($notActive)
->make(true);
$not is the date value pulled in. This produces an ajax data-tables error. I've tried this multiple ways including putting a select statement for the max date in the where clause but it keeps throwing up errors. Is there a better way to query this data that I want? Thanks.
I figured it out! Using the having clause brought up those users who had transactions with dates less than the date that was being provided by the input field (i.e. Not Active Since)
$notActive = DB::table('transactions')
->join('users','transactions.user_id', '=', 'users.user_id' )
->select(DB::raw('users.user_id, users._name, max(transactions.last_posted_date) as last_posted_date'))
->groupBy('users.user_id')
->having('transactions.last_posted_date', '<', ''.$not.')');
return Datatables::of($latestTransactions)
->make(true);