A map field and Blade component for Backpack for Laravel. Click, drag or search to store latitude & longitude, powered by Leaflet.
leafletCRUD field. Click the map, drag the marker or search an address to set a location.- Address search through leaflet-control-geocoder (OpenStreetMap Nominatim, no API key).
<x-leaflet-frontend>Blade component for maps and markers on public pages.- OpenStreetMap by default, with Mapbox available once you add a token.
- Nothing to copy. Views, config and the component are registered by the service provider. Publish only what you want to customise.
- Several maps per form, and maps inside tabs and modals, work out of the box.
| Package | Supported |
|---|---|
| PHP | 8.3 – 8.5 |
| Laravel | 12.x, 13.x |
| Backpack for Laravel | 6.8+, 7.x |
Need PHP 8.2, Laravel 10/11 or Backpack 5? Use an earlier release of this package (
6.x).
composer require siberfx/backpack-leafletjsThe service provider is auto-discovered.
If you want the package to create the coordinate columns, publish and run the migration. It adds lat and lng decimal columns to the table set in backpack.leaflet.table_name:
php artisan vendor:publish --tag=leafletjs-migrations
php artisan migrateName the field after both coordinate columns, separated by a comma. The field renders and saves both inputs itself:
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
CRUD::field([
'name' => 'lat,lng',
'label' => 'Location',
'type' => 'leaflet',
'options' => [
'provider' => 'openstreetmap', // or 'mapbox'. Defaults to config('backpack.leaflet.provider').
'zoom' => 14,
'height' => '400px',
'search' => true, // show the address search box
'clearable' => true, // show a "Clear location" button
'marker_image' => null, // optional custom marker icon URL
],
'hint' => 'Click the map, drag the marker or search an address.',
]);Make sure both columns are fillable on your model:
protected $fillable = ['lat', 'lng'];LeafletCrud adds the field using the column names from the config. Extra field settings such as tab, label or hint are merged in:
use Siberfx\Leafletjs\Http\Controllers\Admin\Traits\LeafletCrud;
class PlaceCrudController extends CrudController
{
use LeafletCrud;
protected function setupCreateOperation(): void
{
$this->setLeafletFields(['tab' => 'General']);
}
}Legacy setup: a map field that writes into your own hidden inputs
With a single-name field, the map writes into inputs whose ids are {name}-lat and {name}-lng:
CRUD::field(['name' => 'leafletMapId', 'type' => 'leaflet']);
CRUD::field(['name' => 'lat', 'type' => 'hidden', 'attributes' => ['id' => 'leafletMapId-lat']]);
CRUD::field(['name' => 'lng', 'type' => 'hidden', 'attributes' => ['id' => 'leafletMapId-lng']]);Show a map on any Blade page:
<x-leaflet-frontend
id="office-map"
:center-point="[52.3676, 4.9041]"
:markers="[
[52.3676, 4.9041],
['lat' => 52.0907, 'lng' => 5.1214, 'popup' => 'Utrecht office'],
]"
:zoom-level="9"
:max-zoom-level="18"
provider="openstreetmap"
height="450px"
class="rounded shadow"
/>Every attribute is optional. Without center-point, the map fits all markers, or centres on default_center when there are none. Without zoom-level, it uses default_zoom. Popup text is escaped. tile-host still works as an alias of provider.
The defaults work without publishing anything. To customise them:
php artisan vendor:publish --tag=leafletjs-configThis creates config/backpack/leaflet.php:
return [
'table_name' => 'settings', // existing table the migration adds lat/lng to; set it to your own table
'lat_field' => 'lat',
'lng_field' => 'lng',
'default_center' => ['lat' => 53.8965741, 'lng' => 27.547158],
'default_zoom' => 14,
'provider' => env('LEAFLET_PROVIDER', env('MAPS_MAPBOX_ACCESS_TOKEN') ? 'mapbox' : 'openstreetmap'),
'providers' => [/* openstreetmap and mapbox. Add your own tile servers here. */],
'assets' => [/* Leaflet and geocoder CDN URLs */],
];Add your token to .env and the mapbox provider is used automatically:
MAPS_MAPBOX_ACCESS_TOKEN=pk.your-token
MAPS_MAPBOX_STYLE=mapbox/streets-v12If mapbox is selected without a token, the map falls back to OpenStreetMap.
php artisan vendor:publish --tag=leafletjs-viewsPublished views go to resources/views/vendor/leafletjs and take precedence over the package views.
composer install
composer test # PHPUnit on Orchestra Testbench
composer analyse # PHPStan with Larastan
composer lint # Pint style check (use `composer format` to fix)CI runs the tests on PHP 8.3, 8.4 and 8.5 against Laravel 12 and 13.
See CHANGELOG.md for release notes and the upgrade guide from 6.x.
If you discover a security issue, please email [email protected] instead of opening a public issue.
The MIT License (MIT). See LICENSE.md for details.
