PHPackages                             encima-io/nova-coordinates-postgis-fields - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. encima-io/nova-coordinates-postgis-fields

ActiveLibrary[Database &amp; ORM](/categories/database)

encima-io/nova-coordinates-postgis-fields
=========================================

A Laravel Nova field for updating PostGIS geography and geometry columns.

v1.0.0(5y ago)21.6k↓55.6%1MITPHPPHP &gt;=7.1.0

Since Oct 15Pushed 5y agoCompare

[ Source](https://github.com/encima-io/nova-coordinates-postgis-fields)[ Packagist](https://packagist.org/packages/encima-io/nova-coordinates-postgis-fields)[ RSS](/packages/encima-io-nova-coordinates-postgis-fields/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (1)DependenciesVersions (2)Used By (0)

Laravel Nova PostGIS Coordinates Field
======================================

[](#laravel-nova-postgis-coordinates-field)

[![Latest Version on Packagist](https://camo.githubusercontent.com/d3202e39e2677418d81109415a29ebd0b8599f78df8edf8d427b4dcb16e9b3c5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f656e63696d612d696f2f6e6f76612d636f6f7264696e617465732d706f73746769732d6669656c64732e737667)](https://packagist.org/packages/encima-io/nova-coordinates-postgis-fields)[![License](https://camo.githubusercontent.com/71b79ac323d67844092fc74359898ad7483341764bb1e3b7204e2c2c23dadebb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f656e63696d612d696f2f6e6f76612d636f6f7264696e617465732d706f73746769732d6669656c64732e737667)](https://packagist.org/packages/encima-io/nova-coordinates-postgis-fields)[![Total Downloads](https://camo.githubusercontent.com/70f27246c773ddb4376fb5edb782edfde9fa26c07c5ff00f4f7f019af8d8c034/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f656e63696d612d696f2f6e6f76612d636f6f7264696e617465732d706f73746769732d6669656c64732e737667)](https://packagist.org/packages/encima-io/nova-coordinates-postgis-fields)

Encima is a web-development team based in Oslo, Norway. You'll find more information about us [on our website](https://e2consult.no).

This package is made to allow you to easily view and update your PostGIS "geography" and "geometry" fields with Laravel Nova. To use this package you should of course already be using PostgreSQL with the PostGIS extension.

Installation
------------

[](#installation)

NB! This package is designed to be used for a POINT, a single location. If you are looking for something to update lines or multiple positions, then this package is not for you.

You can install the package via composer:

```
composer require encima-io/nova-coordinates-postgis-fields
```

Usage
-----

[](#usage)

### Migrations

[](#migrations)

You should have a column on the models table that is of the "geometry" type. This will create a "geography" type column on your table in the PostgreSQL database. This package also supports the "geometry" type.

```
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

public function up()
{
    Schema::create('locations', function (Blueprint $table) {
        // ...
        $table->string('street_address')->nullable();
        $table->geometry('position')->nullable(); // Creates a geography type column
        // ...
    });
}
```

### Resource

[](#resource)

On the Nova resource you should use the `Encima\PostGIS\Latitude` and `Encima\PostGIS\Longitude` fields in addition to the standard `Laravel\Nova\Fields\Place` field.

The values passed to the `latitude()` and `longitude()` methods on the `Place` field must be the same values as you pass to the second parameter in the `Latitude` and `Longitude` fields, see the example below. This is the ensures that the coordinates fetched by the Algolia API will get sent to the `Latitude` and `Longitude` fields.

```
use Encima\PostGIS\Latitude;
use Encima\PostGIS\Longitude;
use Laravel\Nova\Fields\Place;

Place::make('Street address', 'street_address')
    ->latitude('latitude')
    ->longitude('longitude'),
Latitude::make('Latitude', 'latitude'),
Longitude::make('Longitude', 'longitude'),
```

### Custom key-names on fields

[](#custom-key-names-on-fields)

If you however use the `latitude` and `longitude` keys for something else on you model, like an accessor or a column in your database, then you should change the keys to something thats available. When you use custom keys you need to let the fields know about their siblings field-key like this.

```
// latitude with the alias : 'lat_property'
// longitude with the alias : 'lon_propery'
Place::make('Street address', 'street_address')
    ->latitude('lat_property')
    ->longitude('lon_propery'),

// We inform the Latitude-field about its sibling key, longitude, through
// the longitude method. The key should match the key used in the Place field
Latitude::make('Latitude', 'lat_property')
    ->longitude('lon_propery'),

// We do the same, but now for the latitude method
Longitude::make('Longitude', 'lon_propery')
    ->latitude('lat_property'),
```

### Column name

[](#column-name)

By default this package assumes your geography/geometry field is named `position`. If you are using another name for he column then you can specify this through the `fromPosition(string $column)` method. Here is an example, using the column name `location`:

```
Place::make('Street address', 'street_address')
    ->latitude('latitude')
    ->longitude('longitude'),
Latitude::make('Latitude', 'latitude')
    ->fromPosition('location'),
Longitude::make('Longitude', 'longitude')
    ->fromPosition('location'),
```

### Geometry

[](#geometry)

If your position column is of the geometry type, then you need to specify this through the `dataType(string $name)` method.

```
Latitude::make('Latitude', 'latitude')
    ->dataType('geometry'),
Longitude::make('Longitude', 'longitude')
    ->dataType('geometry'),
```

Cache
-----

[](#cache)

This package uses Laravels Cache facade, `\Illuminate\Support\Facades\Cache`, to reduce the amount of queries to the database. Since the coordinates needs a little trip to the database to be converted to the right format, we cache the result by default for 5 minutes, or until they are updated from the edit page.

For caching it uses your default cache store. You can change the cache store by with the `cacheStore(string $store)` method. You can also change the lifetime of the cache by using the `cacheFor($ttl)` method.

```
Place::make('Street address', 'street_address')
    ->latitude('latitude')
    ->longitude('longitude'),
Latitude::make('Latitude', 'latitude')
    ->cacheStore('file')
    ->cacheFor('300 seconds'),
Longitude::make('Longitude', 'longitude')
    ->cacheStore('array')
    ->cacheFor(now()->addDay()),
```

Example
-------

[](#example)

Full example for a resource:

```
