= ? AND DATE(created_at) <= ? GROUP BY DATE(created_at), hittype ORDER BY created_at ASC', [ $start, $end ]); return $result; } catch (\Exception $e) { throw $e; } } /** * Get all referrers of a given range * * @param $start * @param $end * @return Asatru\Database\Collection * @throws \Exception */ public static function getReferrers($start, $end) { try { $items = HitsModel::raw('SELECT DISTINCT referrer, COUNT(referrer) AS count FROM `' . self::tableName() . '` WHERE DATE(created_at) >= ? AND DATE(created_at) <= ? GROUP BY referrer ORDER BY count DESC', [ $start, $end ]); $result = []; foreach ($items as $item) { if (!static::referrer_exists($item->get('referrer'), $result)) { $entry['ref'] = $item->get('referrer'); $entry['count'] = $item->get('count'); $result[] = $entry; } } return $result; } catch (\Exception $e) { throw $e; } } /** * Check if referrer has already been added to the array * * @param $needle * @param $haystack * @return bool */ private static function referrer_exists($needle, $haystack) { foreach ($haystack as $item) { if ((isset($item['ref']) && ($item['ref'] === $needle))) { return true; } } return false; } /** * Get initial start date * * @return string */ public static function getInitialStartDate() { $data = HitsModel::raw('SELECT created_at FROM `' . self::tableName() . '` WHERE id = 1')->first(); return $data->get('created_at'); } /** * Return the associated table name of the migration * * @return string */ public static function tableName() { return 'hits'; } }