'required', 'code' => 'unique:rainlab_location_countries', ]; /** * @var array Relations */ public $hasMany = [ 'states' => ['RainLab\Location\Models\State'] ]; /** * @var bool Indicates if the model should be timestamped. */ public $timestamps = false; /** * @var array Cache for nameList() method */ protected static $nameList = null; public static function getNameList() { if (self::$nameList) { return self::$nameList; } return self::$nameList = self::isEnabled()->orderBy('is_pinned', 'desc')->orderBy('name', 'asc')->lists('name', 'id'); } public static function formSelect($name, $selectedValue = null, $options = []) { return Form::select($name, self::getNameList(), $selectedValue, $options); } public function scopeIsEnabled($query) { return $query->where('is_enabled', true); } public static function getDefault() { return ($defaultId = Setting::get('default_country')) ? static::find($defaultId) : null; } /** * Attempts to find a country from the IP address. * @param string $ipAddress * @return self */ public static function getFromIp($ipAddress) { try { $body = (string) Http::get('http://ip2c.org/?ip='.$ipAddress); if (substr($body, 0, 1) === '1') { $code = explode(';', $body)[1]; return static::where('code', $code)->first(); } } catch (Exception $e) {} } }