Query Builder
This section will go over how to use the spatial features added to Laravel's query builder.
EloquentQueryBuilder::selectDistance()
The selectDistance
method allows you to select the calculated distance from a provided point
. The returned value is in degrees
.
Returns: \Anomaly\Streams\Platform\Model\EloquentQueryBuilder
Arguments
Key | Required | Type | Default | Description |
---|---|---|---|---|
$field | true | string | none | The geocoder field slug you would like to select distance for. |
$point | true | mixed | none | An address, lat/lng array, or `Point` instance. |
$formatted | false | bool | false | A flag to use the formatted point instead of the adjusted marker point. |
$as | false | string | {$field}_distance | An optional name for the selected distance. |
Example
use Anomaly\Streams\Platform\Support\Length;
$locations = LocationModel::selectDistance('address', 'Davenport, IA')->get();
foreach ($locations as $location) {
echo (new Length($location->address_distance, 'deg'))->miles();
}
$closest = LocationModel::selectDistance('address', 'Davenport, IA')->orderBy('address_distance', 'ASC')->first();
Twig
{% set locations = entries('example', 'locations').selectDistance('address', 'Davenport, IA').get() %}
{% for location in locations %}
{{ length(location.address_distance, 'deg').miles }}
{% endfor %}
{% set closest = entries('example', 'locations').selectDistance('address', 'Davenport, IA').orderBy('address_distance', 'ASC').first() %}
EloquentQueryBuilder::whereDistance()
The whereDistance
method lets you add where restrictions based on distance.
Returns: \Anomaly\Streams\Platform\Model\EloquentQueryBuilder
Arguments
Key | Required | Type | Default | Description |
---|---|---|---|---|
$field | true | string | none | The geocoder field slug you would like to select distance for. |
$point | true | mixed | none | An address, lat/lng array, or `Point` instance. |
$operator | true | string | none | A valid where operator (=, etc). |
$distance | true | mixed | none | The distance to use in the comparison. Can be interger or decimal. |
$formatted | false | bool | false | A flag to use the formatted point instead of the adjusted marker point. |
Example
$nearby = LocationModel::whereDistance('address', 'Davenport, IA', '15 mi')->get();
Twig
{% set nearby = entries('example', 'locations').whereDistance('address', 'Davenport, IA', '15 mi').get() %}
EloquentQueryBuilder::orWhereDistance()
The orWhereDistance
distance is the same as the above method but uses an OR WHERE
condition instead.