Closed steamn closed 4 months ago
Hi @steamn
I would be grateful if you could share the code written with your database structure.
@mohaphez
Here is the migration:
Schema::create('buildings', function (Blueprint $table) {
$table->id();
$table->string('latitude');
$table->string('longitude');
$table->string('location')->nullable();
other_fields;
});
BuildingResource.php
return $form
->schema([
TextInput::make('latitude'),
TextInput::make('longitude'),
Map::make('location')
->label('location')
->columnSpanFull()
->afterStateUpdated(function (Get $get, Set $set, string|array|null $old, ?array $state): void {
$set('latitude', $state['lat']);
$set('longitude', $state['lng']);
})
->afterStateHydrated(function ($state, $record, Set $set): void {
$set('location', ['latitude' => $record?->latitude, 'longitude' => $record?->longitude]);
})
->extraStyles([
'min-height: 400px',
])
->liveLocation()
->minZoom(2)
->showMarker()
->markerColor("#22c55eff")
->showFullscreenControl()
->showZoomControl()
->draggable()
->detectRetina()
->showMyLocationButton()
->extraTileControl([])
->extraControl([
'zoomDelta' => 1,
'zoomSnap' => 1,
]),
Actions::make([
Action::make('default_location')->label('Default location')
->icon('heroicon-m-map-pin')
->action(function (Set $set, $state, $livewire): void {
$set('location', ['lat' => '43.207678268758', 'lng' => '23.556060791015625']);
$set('latitude', '43.207678268758');
$set('longitude', '23.556060791015625');
$livewire->dispatch('refreshMap');
})
])->verticalAlignment(VerticalAlignment::Start),
->other_fields;
])
->model(Building::class);
}
Edit. The coordinates are saved correctly, but after refreshing the page the map looks like this. The marker is placed not at the coordinates. https://imgur.com/bVhy7Od
You don't need the location
column in database
If you want to save latitude and longitude in separate columns, perhaps this issue could be helpful: #2.
If not, please let me know.
If I remove the location
column from the database I get SQLSTATE[HY000]: General error: 1 no such column: location
error.
Sorry, I still don't get how it should work.
Is there any filament repository with this plugin to inspect the code?
Have you removed the location
field from the $fillable variable in your model?
I've Model::unguard();
in AppServiceProvider boot method so my models fillables are empty.
You need to define $fillable or use the second method and cast the location
.
Whenever I try to change any entry, the map pointer is always in the same position. I can move it and it will correctly display and save the coordinates to the database, but when the page is refreshed, the marker is again in the center of the map (in the same position).
I think this is because $record from
afterStateHydrated
returns null, but I'm new to filament and don't understand how to properly pass$record->latitude
value.Any help would be appreciated.