encima-io/nova-coordinates-postgis-fields

一个用于更新PostGIS地理和几何列的Laravel Nova字段。

v1.0.0 2020-10-15 21:53 UTC

This package is auto-updated.

Last update: 2024-08-29 05:30:37 UTC


README

Latest Version on Packagist License Total Downloads

Encima是一家位于挪威奥斯陆的web开发团队。您可以在我们的网站上找到更多关于我们的信息 这里

此包旨在让您能够轻松地使用Laravel Nova查看和更新您的PostGIS "地理"和"几何"字段。要使用此包,您当然应该已经使用带有PostGIS扩展的PostgreSQL。

安装

注意!此包设计用于处理一个点,一个单一的位置。如果您正在寻找用于更新线或多个位置的软件,那么此包不适合您。

您可以通过composer安装此包

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

用法

迁移

您应该在模型表上有一个类型为"geometry"的列。这将为您在PostgreSQL数据库中的表创建一个"geography"类型的列。此包也支持"geometry"类型。

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
        // ...
    });
}

资源

在Nova资源中,除了标准的Laravel\Nova\Fields\Place字段外,您还应该使用Encima\PostGIS\LatitudeEncima\PostGIS\Longitude字段。

传递给Place字段上的latitude()longitude()方法的值必须与传递给LatitudeLongitude字段的第二个参数相同的值。请参见下面的示例。这确保了从Algolia API获取的坐标将被发送到LatitudeLongitude字段。

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'),

字段的自定义键名

但是,如果您在模型上使用latitudelongitude键进行其他操作,例如访问器或数据库中的列,则应将键更改为可用的键。当您使用自定义键时,您需要让字段知道其兄弟字段的键名,如下所示。

// 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'),

列名

默认情况下,此包假定您的地理/几何字段名为position。如果您使用其他名称作为列名,则可以通过fromPosition(string $column)方法指定此名称。以下是一个示例,使用列名location

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

几何形状

如果您的位置列是几何类型,则需要通过dataType(string $name)方法指定此信息。

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

缓存

此包使用Laravel的Cache门面\Illuminate\Support\Facades\Cache来减少对数据库的查询次数。由于坐标需要稍微访问数据库以转换为正确的格式,因此我们默认将其缓存5分钟,或者直到它们从编辑页面更新。

对于缓存,它使用您的默认缓存存储。您可以通过cacheStore(string $store)方法更改缓存存储。您还可以通过使用cacheFor($ttl)方法更改缓存的有效期。

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()),

示例

资源的完整示例

<?php

namespace App\Nova;

use Encima\PostGIS\Latitude;
use Encima\PostGIS\Longitude;
use Laravel\Nova\Fields\Number;
use Laravel\Nova\Fields\Place;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Panel;


class UserAddress extends Resource
{

    // ...

    public function fields(Request $request)
    {
        return [
            // Other fields
            // ...
            new Panel('Address Information', [
                Place::make('Street address', 'street_address')
                    ->postalCode('postal_code')
                    ->city('postal_town')
                    ->latitude('latitude')
                    ->longitude('longitude'),
                Text::make('Postal code', 'postal_code')
                    ->sortable(),
                Text::make('Postal town', 'postal_town')
                    ->sortable(),
                Latitude::make('Latitude', 'latitude')
                    ->longitude('longitude')
                    ->fromPosition('position')
                    ->cacheFor('15 minutes'),
                Longitude::make('Longitude', 'longitude')
                    ->latitude('latitude')
                    ->fromPosition('position')
                    ->cacheFor('15 minutes'),
            ])
        ];
    }
}

免责声明

在此包中使用了DB::raw()方法。我们已尝试清理参数,但您永远不知道。请自行承担使用此包的风险!

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件。