Skip to content

Instantly share code, notes, and snippets.

@ericamigo
Last active March 1, 2019 07:03
Show Gist options
  • Save ericamigo/f09353c45b9c3f69d4b64ef2fcdfd00c to your computer and use it in GitHub Desktop.
Save ericamigo/f09353c45b9c3f69d4b64ef2fcdfd00c to your computer and use it in GitHub Desktop.
<?php
use App\Models\Location;
use App\Models\LocationCategory;
/**
* -------------------------------------------------------------------------
* Get location categories
* -------------------------------------------------------------------------
*/
if (! function_exists('get_location_categories'))
{
function get_location_categories()
{
return Cache::remember('all_location_categories', config('cache.cache_minutes'), function () {
return LocationCategory::all();
});
}
}
/**
* -------------------------------------------------------------------------
* Get location category
* -------------------------------------------------------------------------
*/
if (! function_exists('get_location_category'))
{
function get_location_category($key)
{
if (is_numeric($key)) {
return get_location_categories()->where('id', $key)->first();
}
return get_location_categories()->where('slug', $key)->first();
}
}
/**
* -------------------------------------------------------------------------
* Get all locations
* -------------------------------------------------------------------------
*/
if (! function_exists('get_locations'))
{
function get_locations()
{
return Cache::remember('all_locations', config('cache.cache_minutes'), function () {
return Location::all();
});
}
}
/**
* -------------------------------------------------------------------------
* Get all provinces
* -------------------------------------------------------------------------
*/
if (! function_exists('get_provinces'))
{
function get_provinces()
{
return Cache::remember('all_provinces', config('cache.cache_minutes'), function () {
return get_locations()->where('category_id', 2);
});
}
}
/**
* -------------------------------------------------------------------------
* Get provice
* -------------------------------------------------------------------------
*/
if (! function_exists('get_province'))
{
function get_province($key)
{
if (is_numeric($key)) {
return get_provinces()->where('id', $key)->first();
}
return get_provinces()->where('slug', $key)->first();
}
}
/**
* -------------------------------------------------------------------------
* Get all cities
* -------------------------------------------------------------------------
*/
if (! function_exists('get_cities'))
{
function get_cities()
{
return Cache::remember('all_cities', config('cache.cache_minutes'), function () {
return get_locations()->where('category_id', 3);
});
}
}
/**
* -------------------------------------------------------------------------
* Get city
* -------------------------------------------------------------------------
*/
if (! function_exists('get_city'))
{
function get_city($key)
{
if (is_numeric($key)) {
return get_cities()->where('id', $key)->first();
}
return get_cities()->where('slug', $key)->first();
}
}
/**
* -------------------------------------------------------------------------
* Get cities from specific provice
* -------------------------------------------------------------------------
*/
if (! function_exists('get_cities_by_province'))
{
function get_cities_by_province($provinceId)
{
return get_cities()->where('parent_id', $provinceId);
}
}
/**
* -------------------------------------------------------------------------
* Get country
* -------------------------------------------------------------------------
*/
if (! function_exists('get_country'))
{
function get_country($key)
{
if (is_numeric($key)) {
return get_locations()
->where('category_id', 1)
->where('id', $key)
->first();
}
return get_locations()
->where('category_id', 1)
->where('slug', $key)
->first();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment