public function getTotalCities($where)
 {
     $where = strtolower($where);
     $var_cache = 'total' . ucfirst($where) . 'FacebookCities';
     if (!Cache::has($var_cache)) {
         switch ($where) {
             case 'world':
                 $data['total'] = FacebookCity::count();
                 break;
             case 'hispanic':
                 $data['total'] = FacebookCity::whereIdiom('es')->count();
                 break;
             default:
                 $country_idiom = FacebookCountry::whereIdiom($where)->first();
                 if ($country_idiom) {
                     $data['total'] = FacebookCity::whereIdiom($where)->count();
                 } else {
                     $country_search = FacebookCountry::whereCode(FacebookCountry::whereSlug($where)->first(['code'])->code)->first();
                     if ($country_search) {
                         $data['total'] = FacebookCity::whereCountryCode($country_search->code)->count();
                     } else {
                         return 'Invalid method';
                     }
                 }
         }
         Cache::put($var_cache, $data, 1440);
     }
     return Cache::get($var_cache);
 }
 public function getTotalCities($where)
 {
     $var_cache = 'total' . ucfirst($where) . 'FacebookCities';
     if (!Cache::has($var_cache)) {
         switch ($where) {
             case 'world':
                 $data['total'] = FacebookCity::count();
                 break;
             case 'hispanic':
                 $data['total'] = FacebookCity::whereIdiom('es')->count();
                 break;
             default:
                 if (FacebookCountry::whereCode(strtolower($where))->first()) {
                     $data['total'] = FacebookCity::whereCountryCode(strtoupper($where))->count();
                 } else {
                     return 'Invalid method';
                 }
         }
         Cache::put($var_cache, $data, 1440);
     }
     return Cache::get($var_cache);
 }
 public function getCountry($code)
 {
     $var_cache = 'facebookCountry' . $code;
     if (!Cache::has($var_cache)) {
         $data = FacebookCountry::whereCode($code)->first();
         if ($data) {
             $country['id_country'] = $data['id_country'];
             $country['code'] = strtolower($data['code']);
             $country['name'] = $data['name'];
             $country['abbreviation'] = $data['abbreviation'];
             $country['link'] = $this->convert_string_to_url($data['name_en']);
             $country['total_user'] = $this->owloo_number_format($data['total_user']);
             $country['total_female'] = array('value' => $this->owloo_number_format($data['total_female']), 'percent' => $this->owlooFormatPorcent($data['total_female'], $data['total_user']));
             $country['total_male'] = array('value' => $this->owloo_number_format($data['total_male']), 'percent' => $this->owlooFormatPorcent($data['total_male'], $data['total_user']));
             $country['audience_history'] = json_decode($data['audience_history'], true);
             $country['audience_grow']['grow_1'] = $this->formatGrow($data['audience_grow_1'], $data['total_user']);
             $country['audience_grow']['grow_7'] = $this->formatGrow($data['audience_grow_7'], $data['total_user']);
             $country['audience_grow']['grow_30'] = $this->formatGrow($data['audience_grow_30'], $data['total_user']);
             $country['audience_grow']['grow_60'] = $this->formatGrow($data['audience_grow_60'], $data['total_user']);
             $country['audience_grow']['grow_90'] = $this->formatGrow($data['audience_grow_90'], $data['total_user']);
             $country['audience_grow']['grow_180'] = $this->formatGrow($data['audience_grow_180'], $data['total_user']);
             $country['general_ranking'] = $data['general_ranking'];
             /***** Cities *****/
             $country['cities']['supports'] = $data['supports_city'];
             $country['cities']['items'] = array();
             $cities = FacebookCity::whereCountryCode($country['code'])->take(5)->orderBy('total_user', 'DESC')->get(['name', 'total_user']);
             foreach ($cities as $city) {
                 $country['cities']['items'][] = array('name' => $city['name'], 'total_user' => $this->owloo_number_format($city['total_user']), 'percent' => $this->owlooFormatPorcent($city['total_user'], $data['total_user']));
             }
             /***** Regions *****/
             $country['regions']['supports'] = $data['supports_region'];
             $country['regions']['items'] = array();
             $regions = FacebookRegion::whereCountryCode($country['code'])->take(5)->orderBy('total_user', 'DESC')->get(['name', 'total_user']);
             foreach ($regions as $region) {
                 $country['regions']['items'][] = array('name' => $region['name'], 'total_user' => $this->owloo_number_format($region['total_user']), 'percent' => $this->owlooFormatPorcent($region['total_user'], $data['total_user']));
             }
             /***** Ages *****/
             $country['ages']['items'] = array();
             $ages = FacebookCountryAge::whereCountryCode($country['code'])->orderBy('total_user', 'DESC')->get(['name', 'total_user', 'total_female', 'total_male', 'grow_30']);
             foreach ($ages as $age) {
                 $country['ages']['items'][] = array('name' => $age['name'], 'total_user' => $this->owloo_number_format($age['total_user']), 'percent' => $this->owlooFormatPorcent($age['total_user'], $data['total_user']));
             }
             //Get max values
             foreach (array('user', 'female', 'male') as $value) {
                 $country['ages']['max_' . $value] = array();
                 $age_max_total = FacebookCountryAge::whereCountryCode($country['code'])->orderBy('total_' . $value, 'DESC')->orderBy('id', 'ASC')->first(['name', 'total_' . $value]);
                 if (isset($age_max_total['name'])) {
                     $country['ages']['max_' . $value] = array('name' => $age_max_total['name'], 'value' => $this->owloo_number_format($age_max_total['total_' . $value]), 'percent' => $this->owlooFormatPorcent($age_max_total['total_' . $value], $data['total_user']));
                 }
             }
             //Get Trend-up
             $country['ages']['trend_up'] = array();
             $age_grow = FacebookCountryAge::whereCountryCode($country['code'])->orderBy('grow_30', 'DESC')->orderBy('id', 'ASC')->first(['name', 'total_user', 'grow_30']);
             if (isset($age_grow['name']) && $age_grow['grow_30'] > 0) {
                 $country['ages']['trend_up'] = array('name' => $age_grow['name'], 'grow' => $this->formatGrow($age_grow['grow_30'], $age_grow['total_user']));
             }
             /***** Languages *****/
             $country['languages']['items'] = array();
             $languages = FacebookCountryLanguage::whereCountryCode($country['code'])->orderBy('total_user', 'DESC')->get(['name', 'total_user', 'grow_30']);
             foreach ($languages as $language) {
                 $country['languages']['items'][] = array('name' => $language['name'], 'total_user' => $this->owloo_number_format($language['total_user']), 'percent' => $this->owlooFormatPorcent($language['total_user'], $data['total_user']));
             }
             //Get the Trend-up
             $country['languages']['trend_up'] = array();
             $language_grow = FacebookCountryLanguage::whereCountryCode($country['code'])->orderBy('grow_30', 'DESC')->orderBy('total_user', 'DESC')->first(['name', 'total_user', 'grow_30']);
             if (isset($language_grow['name']) && $language_grow['grow_30'] > 0) {
                 $country['languages']['trend_up'] = array('name' => $language_grow['name'], 'grow' => $this->formatGrow($language_grow['grow_30'], $language_grow['total_user']));
             }
             /***** Relationships *****/
             $country['relationships']['items'] = array();
             $relationships = FacebookCountryRelationship::whereCountryCode($country['code'])->orderBy('total_user', 'DESC')->get(['name', 'total_user', 'total_user_grow_30', 'total_female_grow_30', 'total_male_grow_30']);
             foreach ($relationships as $relationship) {
                 $country['relationships']['items'][] = array('name' => $relationship['name'], 'total_user' => $this->owloo_number_format($relationship['total_user']), 'percent' => $this->owlooFormatPorcent($relationship['total_user'], $data['total_user']));
             }
             //Get Trend-up
             foreach (array('user', 'female', 'male') as $value) {
                 $country['relationships']['trend_up_' . $value] = array();
                 $relationship_grow = FacebookCountryRelationship::whereCountryCode($country['code'])->orderBy('total_' . $value . '_grow_30', 'DESC')->orderBy('total_user', 'DESC')->first(['name', 'total_' . $value, 'total_' . $value . '_grow_30']);
                 if (isset($relationship_grow['name']) && $relationship_grow['total_' . $value . '_grow_30'] > 0) {
                     $country['relationships']['trend_up_' . $value] = array('name' => $relationship_grow['name'], 'grow' => $this->formatGrow($relationship_grow['total_' . $value . '_grow_30'], $relationship_grow['total_' . $value]));
                 }
             }
             /***** Interests *****/
             $country['interests']['items'] = array();
             $interests = FacebookCountryInterest::whereCountryCode($country['code'])->whereNivel(1)->orderBy('total_user', 'DESC')->get(['id_interest', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
             $count_nivel_1 = 0;
             foreach ($interests as $interest) {
                 $country['interests']['items'][$count_nivel_1] = array('name' => $interest['name'], 'total_user' => $this->owloo_number_format($interest['total_user']), 'percent' => $this->owlooFormatPorcent($interest['total_user'], $data['total_user']), 'items' => array());
                 $interests_childrens_1 = FacebookCountryInterest::whereCountryCode($country['code'])->whereNivelSuperior($interest['id_interest'])->orderBy('total_user', 'DESC')->take(5)->get(['id_interest', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
                 $count_nivel_2 = 0;
                 foreach ($interests_childrens_1 as $children_1) {
                     $country['interests']['items'][$count_nivel_1]['items'][$count_nivel_2] = array('name' => $children_1['name'], 'total_user' => $this->owloo_number_format($children_1['total_user']), 'percent' => $this->owlooFormatPorcent($children_1['total_user'], $data['total_user']), 'items' => array());
                     $interests_childrens_2 = FacebookCountryInterest::whereCountryCode($country['code'])->whereNivelSuperior($children_1['id_interest'])->orderBy('total_user', 'DESC')->take(5)->get(['id_interest', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
                     foreach ($interests_childrens_2 as $children_2) {
                         $country['interests']['items'][$count_nivel_1]['items'][$count_nivel_2]['items'][] = array('name' => $children_2['name'], 'total_user' => $this->owloo_number_format($children_2['total_user']), 'percent' => $this->owlooFormatPorcent($children_2['total_user'], $data['total_user']));
                     }
                     $count_nivel_2++;
                 }
                 $count_nivel_1++;
             }
             //Get the Trend-up
             $country['interests']['trend_up'] = array();
             $interest_grow = FacebookCountryInterest::whereCountryCode($country['code'])->whereNivel(1)->orderBy('grow_30', 'DESC')->orderBy('total_user', 'DESC')->first(['name', 'total_user', 'grow_30']);
             if (isset($interest_grow['name']) && $interest_grow['grow_30'] > 0) {
                 $country['interests']['trend_up'] = array('name' => $interest_grow['name'], 'grow' => $this->formatGrow($interest_grow['grow_30'], $interest_grow['total_user']));
             }
             /***** Comportamientos *****/
             $country['comportamientos']['items'] = array();
             $comportamientos = FacebookCountryComportamiento::whereCountryCode($country['code'])->whereNivel(1)->orderBy('total_user', 'DESC')->get(['id_comportamiento', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
             $count_nivel_1 = 0;
             foreach ($comportamientos as $comportamiento) {
                 $country['comportamientos']['items'][$count_nivel_1] = array('name' => $comportamiento['name'], 'total_user' => $this->owloo_number_format($comportamiento['total_user']), 'percent' => $this->owlooFormatPorcent($comportamiento['total_user'], $data['total_user']), 'items' => array());
                 $comportamientos_childrens_1 = FacebookCountryComportamiento::whereCountryCode($country['code'])->whereNivelSuperior($comportamiento['id_comportamiento'])->orderBy('total_user', 'DESC')->take(5)->get(['id_comportamiento', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
                 $count_nivel_2 = 0;
                 foreach ($comportamientos_childrens_1 as $children_1) {
                     $country['comportamientos']['items'][$count_nivel_1]['items'][$count_nivel_2] = array('name' => $children_1['name'], 'total_user' => $this->owloo_number_format($children_1['total_user']), 'percent' => $this->owlooFormatPorcent($children_1['total_user'], $data['total_user']), 'items' => array());
                     $comportamientos_childrens_2 = FacebookCountryComportamiento::whereCountryCode($country['code'])->whereNivelSuperior($children_1['id_comportamiento'])->orderBy('total_user', 'DESC')->take(5)->get(['id_comportamiento', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
                     $count_nivel_3 = 0;
                     foreach ($comportamientos_childrens_2 as $children_2) {
                         $country['comportamientos']['items'][$count_nivel_1]['items'][$count_nivel_2]['items'][$count_nivel_3] = array('name' => $children_2['name'], 'total_user' => $this->owloo_number_format($children_2['total_user']), 'percent' => $this->owlooFormatPorcent($children_2['total_user'], $data['total_user']));
                         $comportamientos_childrens_3 = FacebookCountryComportamiento::whereCountryCode($country['code'])->whereNivelSuperior($children_2['id_comportamiento'])->orderBy('total_user', 'DESC')->take(5)->get(['id_comportamiento', 'name', 'total_user', 'total_female', 'total_male', 'grow_30']);
                         foreach ($comportamientos_childrens_3 as $children_3) {
                             $country['comportamientos']['items'][$count_nivel_1]['items'][$count_nivel_2]['items'][$count_nivel_3]['items'][] = array('name' => $children_3['name'], 'total_user' => $this->owloo_number_format($children_3['total_user']), 'percent' => $this->owlooFormatPorcent($children_3['total_user'], $data['total_user']));
                         }
                         $count_nivel_3++;
                     }
                     $count_nivel_2++;
                 }
                 $count_nivel_1++;
             }
             //Get the Trend-up
             $country['comportamientos']['trend_up'] = array();
             $comportamiento_grow = FacebookCountryComportamiento::whereCountryCode($country['code'])->whereNivel(1)->orderBy('grow_30', 'DESC')->orderBy('total_user', 'DESC')->first(['name', 'total_user', 'grow_30']);
             if (isset($comportamiento_grow['name']) && $comportamiento_grow['grow_30'] > 0) {
                 $country['comportamientos']['trend_up'] = array('name' => $comportamiento_grow['name'], 'grow' => $this->formatGrow($comportamiento_grow['grow_30'], $comportamiento_grow['total_user']));
             }
             print_r($country);
             die;
             Cache::put($var_cache, $country, 1440);
         } else {
             return 'Invalid method';
         }
     }
     return Cache::get($var_cache);
 }