Skip to content

Commit

Permalink
Merge pull request #1 from siberfx/master
Browse files Browse the repository at this point in the history
✨ composer.json and readme.md added
  • Loading branch information
siberfx authored Oct 6, 2024
2 parents 48f9f3a + ebb0704 commit 072aa88
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
vendor
.idea/
141 changes: 141 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
## Leaflet Draw Polygon with mapbox

#### <p align="center">Leaflet Drawing Polygon field and storing as json for Laravel Backpack ^6.x</p>

<p align="center">
<img src="https://github.com/siberfx/siberfx-leaflet-drawjs/raw/main/img/preview.png">
</p>

<img alt="Stars" src="https://img.shields.io/github/stars/siberfx/backpack-leaflet-drawjs?style=plastic&labelColor=343b41"/> <img alt="Forks" src="https://img.shields.io/github/forks/siberfx/backpack-leaflet-drawjs?style=plastic&labelColor=343b41"/>
[![Latest Version on Packagist](https://img.shields.io/packagist/dt/siberfx/backpack-leaflet-drawjs?style=plastic)](https://packagist.org/packages/siberfx/backpack-leaflet-drawjs)

## Installation

You can install the package via composer:

```bash
composer require siberfx/backpack-leaflet-drawjs
```

## Usage
``` php

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

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

```

### Publish files

``` bash
php artisan vendor:publish --provider="Backpack\LeafletDrawjs\LeafLetServiceProvider" --tag="all"
```


### Add Leaflet drawing polygon as json and store as json

you will have to need a migration with json or text type to store the data, as in the example below;
```php
public function up()
{
Schema::create('polygons', function (Blueprint $table) {
$table->id();
$table->json('geojson'); // Store GeoJSON data
$table->timestamps();
});
}

```

the model should be like;


```php
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);
}
}

```

the controller example should be like;

```php
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' => 'required|array'
]);

// Store the polygon data as GeoJSON or serialized data
$polygon = new Polygon();
$polygon->geojson = json_encode($request->polygon); // Save GeoJSON
$polygon->save();

return response()->json(['message' => 'Polygon saved successfully']);
}
}
```
the route example;

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


### Call it inside your controller like this or

or add in your Crud controller manually where you want to see it as shown below.

```php

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

```

```
### Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
## Credits
- [Selim Gormus](https://github.com/siberfx)
47 changes: 47 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "siberfx/backpack-leaflet-drawjs",
"description": "LeafletDrawjs for Laravel Backpack ^6.x",
"keywords": [
"laravel 9.x",
"laravel 10.x",
"laravel 11.x",
"backpack",
"leaflet-drawjs",
"leaflet-polygon",
"leaflet polygon json",
"leafjs",
"openstreetmaps",
"mapbox"
],
"homepage": "https://github.com/siberfx/backpack-leaflet-drawjs",
"license": "MIT",
"type": "library",
"authors": [
{
"name": "Selim Görmüş",
"email": "[email protected]",
"role": "Fullstack Software Engineer"
}
],
"require": {
"php": "^8.1",
"backpack/crud": "^6.0",
"illuminate/support": "^9.0|^10.0|^11.0"
},
"minimum-stability": "dev",
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"Siberfx\\LeafletDrawjs\\LeafLetServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Siberfx\\LeafletDrawjs\\": "src"
}
}
}
Binary file added img/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 072aa88

Please sign in to comment.