Skip to content

Instantly share code, notes, and snippets.

@zacharyblank
Created February 19, 2014 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zacharyblank/9094264 to your computer and use it in GitHub Desktop.
Save zacharyblank/9094264 to your computer and use it in GitHub Desktop.
public function findByVendorIdByMonth($vendor, $id)
{
$data = $this->select(DB::raw("DATE_FORMAT(date, '%Y-%m-01 00:00:00') as date, SUM(views) as views, SUM(uniques) as uniques, SUM(inquiries) as inquiries"))
->groupBy(DB::raw("YEAR(date), MONTH(date)"))
->whereBetween('date', [date('Y-m-d 00:00:00', strtotime('-1 year')), date('Y-m-d 00:00:00', strtotime('today'))])
->where('vendor_type', $vendor)
->where('vendor_id', $id)->get();
$monthly = ['totals' => [
'uniques' => 0,
'views' => 0,
'inquiries' => 0
]];
for ($key = 0; $key < 12; $key++) {
$monthly['totals']['uniques'] += (isset($data[$key])) ? $data[$key]->uniques : 0;
$monthly['totals']['views'] += (isset($data[$key])) ? $data[$key]->views : 0;
$monthly['totals']['inquiries'] += (isset($data[$key])) ? $data[$key]->inquiries : 0;
if (isset($data[$key])) {
$monthly['intervals'][] = $data[$key]->getResourceAttributes();
} else {
$dummy = new Analytics();
$dummy->date = date('Y-m-1 00:00:00', strtotime("-$key months"));
$monthly['intervals'][] = $dummy->getResourceAttributes();
}
}
return $monthly;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment