I want some help with a WHERE clause $start date is set further up in the code, and the query works when running against SQL from within Laravel. I get an error saying that $startdate isn't set. I am not sure that I am even doing WHERE correctly.
$query = DB::query()
->select('CC AS CountOfVisits', DB::raw('count(CC) AS Users'))
->fromSub
(function (\Illuminate\Database\Query\Builder $query) {
$query->select('user_id', DB::raw('count(user_id) AS CC '))
->from('mytable')
->where('created_at', '>=', $startdate)
->groupBy('user_id');
}, 'CC')
->groupBy('CC');
$result = DB::connection('mysql2')->select($query->toSql());
SELECT CC AS CountOfVisits, Count(CC) AS Users FROM ( SELECT user_id, count(user_id) AS CC FROM mytable WHERE created_at <= '2019-10-29' AND created_at >= '2019-10-01' GROUP BY user_id ) AS CC GROUP BY CC;the issue is getting the variable into the function. I triedfunction (\Illuminate\Database\Query\Builder $query) use($startdate) {but that didn't work either