diff --git a/README.md b/README.md index 6a2db6b..c6129ac 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,35 @@ protected static $kilometers = true; const LONGITUDE = 'lng'; ``` +### Options +1. You may pass an array of options as the third parameter of the distance method, these options will allow you to set a new table name or column names at runtime. + ```php + $query = Model::distance($latitude, $longitude, $options); + ``` + +2. There are three fields you set with the options parameter at runtime: table, latitude_column, and longitude_column: + ```php + $options = [ + 'table' => 'coordinates', + 'latitude_column' => 'lat', + 'longitude_column' => 'lon' + ] + + Model::select('id', 'name')->distance($latitude, $longitude, $options); + ``` +3. The table field will allow you to set the table from which the the coordinates will be selected at runtime, allowing you to join the coordinates to your model from another table. + ```php + + Model::join('locations', function($join){ + $join->on('model.id', '=', 'locations.model_id'); + }) + ->select('id', 'name') + ->distance($latitude, $longitude, ['table' => 'locations']); + ``` +4. The latitude_column and longitude_column fields can be used to set the column names for a joined table, or to override the default column names (including those set on your model) at runtime. If you don't set the column name fields at runtime when using a joined table then column names set on your model, or the defaults of 'latitude' and 'longitude' will be used. Setting `const LATITUDE = 'lat'` or `const LONGITUDE = 'lng'` on a joined model will have no effect. + + ## Installation [PHP](https://php.net) 5.6.4+ and [Laravel](http://laravel.com) 5+ are required. diff --git a/src/Geographical.php b/src/Geographical.php index 8a24264..1660ddd 100644 --- a/src/Geographical.php +++ b/src/Geographical.php @@ -16,16 +16,28 @@ */ trait Geographical { + + /** + * Options variable used to store optional parameters set at runtime + * @var array + */ + protected $geographical_options; + /** * Find the distance to all the entries in your table from a particular location. * * @param \Illuminate\Database\Eloquent\Builder $query * @param float $latitude Latitude. * @param float $longitude Longitude. + * @param array|null $options (optional) Array to holds runtime options. + * ['table'] string Set a table name to use instead of the default model table (this allows lat/long to be joined to the query from another table). + * ['latitude_column'] string Set a column name for latitude at runtime + * ['longitude_column'] string Set a column name for longitude at runtime * @return \Illuminate\Database\Eloquent\Builder */ - public function scopeDistance(Builder $query, float $latitude, float $longitude) + public function scopeDistance(Builder $query, float $latitude, float $longitude, $options = []) { + $this->geographical_options = $options; $latName = $this->getQualifiedLatitudeColumn(); $lonName = $this->getQualifiedLongitudeColumn(); @@ -75,6 +87,12 @@ public function scopeGeofence( return $query->havingRaw('distance BETWEEN ? AND ?', [$inner_radius, $outer_radius]); } + protected function getTableName(){ + return isset($this->geographical_options['table']) ? + $this->geographical_options['table'] + : $this->getTable(); + } + /** * Get full sql column name for latitude. * @@ -82,7 +100,7 @@ public function scopeGeofence( */ protected function getQualifiedLatitudeColumn() { - return $this->getConnection()->getTablePrefix() . $this->getTable() . '.' . $this->getLatitudeColumn(); + return $this->getConnection()->getTablePrefix() . $this->getTableName() . '.' . $this->getLatitudeColumn(); } /** @@ -92,7 +110,7 @@ protected function getQualifiedLatitudeColumn() */ protected function getQualifiedLongitudeColumn() { - return $this->getConnection()->getTablePrefix() . $this->getTable() . '.' . $this->getLongitudeColumn(); + return $this->getConnection()->getTablePrefix() . $this->getTableName() . '.' . $this->getLongitudeColumn(); } /** @@ -102,7 +120,17 @@ protected function getQualifiedLongitudeColumn() */ public function getLatitudeColumn() { - return defined('static::LATITUDE') ? static::LATITUDE : 'latitude'; + if(isset($this->geographical_options['latitude_column'])) + { + return $this->geographical_options['latitude_column']; + } + + if(defined('static::LATITUDE')) + { + return static::LATITUDE; + } + + return 'latitude'; } /** @@ -112,6 +140,16 @@ public function getLatitudeColumn() */ public function getLongitudeColumn() { - return defined('static::LONGITUDE') ? static::LONGITUDE : 'longitude'; + if(isset($this->geographical_options['longitude_column'])) + { + return $this->geographical_options['longitude_column']; + } + + if(defined('static::LONGITUDE')) + { + return static::LONGITUDE; + } + + return 'longitude'; } }