PHP code example of siberfx / backpack-leaflet-drawjs

1. Go to this page and download the library: Download siberfx/backpack-leaflet-drawjs library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

siberfx / backpack-leaflet-drawjs example snippets


 CRUD::addField([
            'name' => 'coordinates',
            'label' => 'Poligon coordinates',
            'type' => 'leaflet-draw',
            'options' => [
                'provider' => 'mapbox',
                'marker_image' => null
            ],

        ]);

 public function up()
    {
        Schema::create('polygons', function (Blueprint $table) {
            $table->id();
            $table->json('geojson'); // Store GeoJSON data
            $table->timestamps();
        });
    }


namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Polygon extends Model
{
    use HasFactory;

    protected $fillable = ['geojson'];

    // Optionally, if you want to decode the GeoJSON automatically
    public function getGeojsonAttribute($value)
    {
        return json_decode($value);
    }
}


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Polygon;

class PolygonController extends Controller
{
    public function store(Request $request)
    {
        // Validate the incoming data (optional)
        $request->validate([
            'polygon' => 'ly']);
    }
}

Route::post('/store-polygon', [PolygonController::class, 'store'])->name('store-polygon');


 $this->crud->addField([
        'label' => 'Location',
        'name' => 'location',
        'type' => 'leaflet-draw',
        'options' => [
            'provider' => 'mapbox',
            'marker_image' => null
        ],
        'tab' => 'General'
        'hint' => '<em>You can also drag and adjust your mark by clicking</em>'
 ]);

 php

// config/leaflet.php file content, you can modify it by your own settings.
return [

    'mapbox' => [
        'access_token' => env('MAPS_MAPBOX_ACCESS_TOKEN', 'xxxxxxxxxxxxxxxxxxxxx'),
    ],
];

json
{"type": "Feature", "geometry": {"type": "Polygon", "coordinates": [[[30.59967, 36.832371], [30.617523, 36.899391], [30.669708, 36.904882], [30.702667, 36.91696], [30.768585, 36.910372], [30.809784, 36.878522], [30.838623, 36.853252], [30.775452, 36.848857], [30.730133, 36.856549], [30.697174, 36.878522], [30.662842, 36.885113], [30.59967, 36.832371]]]}, "properties": []}