Skip to content

Commit

Permalink
Merge pull request #34 from amorphia/join-support
Browse files Browse the repository at this point in the history
Add support for joined tables
  • Loading branch information
malhal authored Feb 14, 2023
2 parents 642d080 + d5f13ea commit cd8252c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
48 changes: 43 additions & 5 deletions src/Geographical.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -75,14 +87,20 @@ 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.
*
* @return string
*/
protected function getQualifiedLatitudeColumn()
{
return $this->getConnection()->getTablePrefix() . $this->getTable() . '.' . $this->getLatitudeColumn();
return $this->getConnection()->getTablePrefix() . $this->getTableName() . '.' . $this->getLatitudeColumn();
}

/**
Expand All @@ -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();
}

/**
Expand All @@ -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';
}

/**
Expand All @@ -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';
}
}

0 comments on commit cd8252c

Please sign in to comment.