spiritsaint / nova-map-field
Map Field for Laravel Nova
v1.0.2
2022-10-22 07:36 UTC
Requires
- illuminate/support: ^8.0|^9.0
- matanyadaev/laravel-eloquent-spatial: ^2.0.1
This package is auto-updated.
Last update: 2025-01-29 10:42:01 UTC
README
Using this package, you can use spatial fields in Laravel Nova.
🚀 If you find this project interesting, please consider supporting me on the open source journey
Requirements:
- PHP 8.0.2 or higher
- Laravel 8 or higher
Installation
-
Install the package via composer:
composer require mostafaznv/nova-map-field
-
Publish config and assets:
php artisan vendor:publish --provider="Mostafaznv\NovaMapField\NovaMapFieldServiceProvider"
-
Done
Usage
-
Create table with spatial fields
<?php return new class extends Migration { public function up() { Schema::create('locations', function (Blueprint $table) { $table->id(); $table->string('title', 150); $table->point('location')->nullable(); $table->polygon('area')->nullable(); $table->multiPolygon('areas')->nullable(); $table->timestamps(); }); } };
-
Add
HasSpatialColumns
trait to model<?php namespace App\Models; use Mostafaznv\NovaMapField\Traits\HasSpatialColumns; class Location extends Model { use HasSpatialColumns; }
-
Define spatial columns of model
<?php namespace App\Models; use MatanYadaev\EloquentSpatial\Objects\MultiPolygon; use MatanYadaev\EloquentSpatial\Objects\Point; use MatanYadaev\EloquentSpatial\Objects\Polygon; class Location extends Model { use HasSpatialColumns; protected $casts = [ 'location' => Point::class, 'area' => Polygon::class, 'areas' => MultiPolygon::class ]; }
-
Add map fields to resource
<?php namespace App\Nova\Resources; use Mostafaznv\NovaMapField\Fields\MapMultiPolygonField; use Mostafaznv\NovaMapField\Fields\MapPointField; use Mostafaznv\NovaMapField\Fields\MapPolygonField; class Location extends Resource { public function fields(Request $request): array { return [ MapPointField::make('location'), MapPolygonField::make('area'), MapMultiPolygonField::make('areas'), ]; } }
-
Done
Map Field Methods
Config Properties
Using Spatial Columns over Application
This package uses Laravel Eloquent Spatial under the hood. to use columns and querying them over the application, please read Laravel Eloquent Spatial documentation
Complete Example
<?php namespace App\Nova\Resources; use App\Nova\Resource; use Illuminate\Http\Request; use Laravel\Nova\Fields\ID; use Laravel\Nova\Fields\Text; use App\Models\Location as Model; use Mostafaznv\NovaMapField\DTOs\MapSearchBoxType; use Mostafaznv\NovaMapField\DTOs\MapSearchProvider; use Mostafaznv\NovaMapField\Fields\MapPointField; class Location extends Resource { public static string $model = Model::class; public function fields(Request $request): array { return [ ID::make()->sortable(), Text::make('Title') ->sortable() ->rules('required', 'max:255'), MapPointField::make(trans('Location'), 'location') ->defaultLatitude(35.6978527) ->defaultLongitude(51.4037269) ->zoom(14) ->withoutZoomControl() ->withoutZoomSlider() ->withFullScreenControl() ->mapHeight(360) ->hideDetailButton(false) ->markerIcon(3) ->searchProvider(MapSearchProvider::OSM()) ->searchProviderApiKey('api-key') ->withAutocompleteSearch() ->searchAutocompleteMinLength(4) ->searchAutocompleteTimeout(500) ->searchLanguage('fa-IR') ->searchPlaceholder('Placeholder ...') ->searchBoxType(MapSearchBoxType::BUTTON()) ->searchResultLimit(3) ->searchResultKeepOpen(true) ->required() ->requiredOnCreate() ->requiredOnUpdate() ->stacked(), ]; } }
🚀 If you find this project interesting, please consider supporting me on the open source journey
License
This software is released under The MIT License (MIT).