lbcdev/filament-map-field
Filament Map Field adds Leaflet-powered map components for Filament v3/v4: MapField and MapEntry to pick/display coordinates, plus MapBoundsField/Entry for rectangular areas. Supports reactive updates, separate lat/lng fields, and nested JSON paths.
Installation
composer require lbcdev/filament-map-field
Publish config (if needed):
php artisan vendor:publish --provider="Lbcdev\FilamentMapField\FilamentMapFieldServiceProvider" --tag="config"
First Use Case Add a map field to a Filament form:
use Lbcdev\FilamentMapField\Forms\Components\MapField;
MapField::make('coordinates')
->label('Location')
->columnSpanFull()
->required()
->default([
'lat' => 40.7128, // Default latitude
'lng' => -74.0060, // Default longitude
]);
Where to Look First
src/Forms/Components/ and src/Resources/ViewComponents/ for customization hooks.config/filament-map-field.php for global settings (e.g., default map provider).// For a single coordinate field (lat/lng stored as separate columns)
MapField::make('location')
->latColumn('latitude')
->lngColumn('longitude')
->mapOptions([
'zoom' => 12,
'center' => [40.7128, -74.0060],
]);
// Store coordinates in a JSON column (e.g., `location` column)
MapField::make('location')
->jsonColumn('location')
->default(['lat' => 34.0522, 'lng' => -118.2437]);
use Lbcdev\FilamentMapField\Forms\Components\MapBoundsField;
MapBoundsField::make('search_area')
->northEastColumn('northeast')
->southWestColumn('southwest')
->mapOptions(['zoom' => 8]);
use Lbcdev\FilamentMapField\Resources\ViewComponents\MapEntry;
MapEntry::make('coordinates')
->latColumn('latitude')
->lngColumn('longitude')
->openInNewTab()
->mapOptions(['zoom' => 15]);
Use a closure for dynamic defaults (e.g., user's current location):
MapField::make('address')
->default(fn () => [
'lat' => request()->user()->last_latitude ?? 40.7128,
'lng' => request()->user()->last_longitude ?? -74.0060,
]);
Ensure your model has the correct columns:
// For separate lat/lng columns
protected $fillable = ['latitude', 'longitude'];
// For JSON storage
protected $casts = [
'location' => 'array',
];
Add validation rules to your form:
->rules([
'coordinates.lat' => 'required|numeric|between:-90,90',
'coordinates.lng' => 'required|numeric|between:-180,180',
]);
Override the default provider (e.g., switch from OpenStreetMap to Google Maps):
MapField::make('map')
->mapOptions([
'provider' => 'google',
'apiKey' => config('services.google_maps.api_key'),
]);
Translate labels and placeholders:
->label(__('Location'))
->placeholder(__('Search for a location...'));
Show/hide the map based on other fields:
->visible(fn ($record) => $record->is_premium_user)
Undefined column errors when using latColumn()/lngColumn().jsonColumn() instead.$fillable (for separate columns) or properly cast (for JSON). Example:
// Model
protected $fillable = ['coordinates'];
protected $casts = ['coordinates' => 'array'];
mapOptions includes a valid provider (e.g., 'provider' => 'openstreetmap').Undefined index when using dot notation (e.g., 'location.lat').jsonColumn(). Example:
// Correct: Column is `location` (JSON), and you access `location.lat`
MapField::make('location')
->jsonColumn('location')
->latPath('lat') // Explicitly define paths
->lngPath('lng');
// Filament v4
use Lbcdev\FilamentMapField\Forms\Components\MapField;
Add this to your mapOptions to debug:
->mapOptions([
'debug' => true, // Logs options to Laravel logs
'zoom' => 10,
]);
The package wraps lbcdev-map. If issues persist:
wire:ignore misconfigurations).After installing or updating:
php artisan optimize:clear
php artisan view:clear
Override the default marker icon:
->mapOptions([
'markerIcon' => 'https://example.com/custom-marker.png',
]);
Use Filament’s CSS utilities or inline styles:
->columnSpan('full')
->extraAttributes(['style' => 'height: 400px;']);
Listen for coordinate changes:
->extraAttributes(['wire:ignore' => 'false'])
->mapOptions([
'events' => [
'moveend' => 'updateCoordinates',
],
])
->script(<<<'JS'
document.addEventListener('livewire:init', () => {
Livewire.on('updateCoordinates', (coords) => {
@this.set('coordinates', coords);
});
});
JS);
Extend the package to support additional map providers:
namespace App\Providers;
use Lbcdev\FilamentMapField\Contracts\MapProvider;
use Illuminate\Support\ServiceProvider;
class CustomMapProviderServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind(MapProvider::class, function () {
return new class implements MapProvider {
public function getScript(): string
{
return '<script src="https://custom-maps.com/script.js"></script>';
}
};
});
}
}
config/app.php.Publish and modify the package’s views:
php artisan vendor:publish --tag="filament-map-field-views"
Edit files in resources/views/vendor/filament-map-field/.
How can I help you explore Laravel packages today?