select('account_id', DB::raw('count(*) as total_posts')) // ->groupBy('account_id') // ->get(); $datasets = DB::table('documents') ->select('id', 'account_id') ->where('server_state', 'approved') ->whereRaw('server_date_modified < DATEADD(day,-14,GETDATE())') ->get(); // update statistics table foreach ($datasets as $dataset) { // DB::table('users_statistics') // ->where('user_id', $dataset->user_id) // ->update(['total_datasets' => $dataset->total_posts]); DB::table('documents') ->where('id', $dataset->id) ->update([ 'reject_reviewer_note' => 'Dataset was automatically rejected because of the time limit', 'server_state' => 'rejected_reviewer', 'server_date_modified' => DB::raw('now()') ]); } } }