Keep LAT and LONG values when editing
chartalex opened this issue · 15 comments
Hi all
Only when editing, it looks like latitude and longitude values are lost, and it shows a random map (with random location and marker).
So I need to type again the entire address to reset the map to the correct values, even if I wanted to edit another field.
Did I make something wrong, or this feature is not developed yet?
Thanks a lot for your help
@chartalex This may be a bug, but I haven't noticed it when I am using it.
Can you post the code you are using in your Nova resource to show the field?
Thanks @mikebronner for your help on this
Here is my "rdv" (rendez-vous) ressource:
public function fields(Request $request)
{
return [
ID::make()->hideFromIndex(),
$this->addressFields(),
];
}
protected function addressFields()
{
return $this->merge([
Text::make('Département', 'dpt')->sortable(),
Text::make('Ville','city'),
Text::make('Nom','name'),
MapMarker::make("Position")->hideFromIndex()
->latitude('lat')
->longitude('lng'),
]);
}
And here the structure of my rdvs table:
Schema::create('rdvs', function (Blueprint $table) { $table->bigIncrements('id');
$table->text('name');
$table->text('city');
$table->text('dpt');
$table->text('lat');
$table->text('lng');
$table->string('details');
$table->timestamps();
});
Thanks for the additional info. Looking into it now. :)
@chartalex This should now be fixed in release 0.1.7. Thanks for reporting it! Please let me know how ti works for you.
Hello Mike,
Bumping on this ticket again, I have updated to 0.1.15 but am still experiencing the same as OP.
PHP Version: ^7.4
Nova Version: ^2 (currently 2.11.1)
Laravel Version: ^6
I'm using the MapMarker within a callable for pivot fields:
new Panel('Visites', [
BelongsToMany::make('Visites', 'visits', Visit::class)
->fields(fn() => [
Number::make("Étape N°", 'sort_order'),
MapMarker::make('Coordonnées')
->latitude('latitude')
->longitude('longitude')
->defaultZoom(15)
->defaultLatitude(46.517358)
->defaultLongitude(6.629186),
]),
]),
What works:
What doesn't work:
- Relation view on resource displays a "-" on the "coordonnées" (coordinates) field
- Re-editing the attached model resets the MapMarker to the default Latitude & Longitude
Am I doing something wrong?
Thank you for your help :-)
@phlisg What version of this package is installed in your Laravel app? Run:
composer show GeneaLabs/nova-map-marker-field
I'll try to replicate the issue in the mean time.
@phlisg Can you provide a basic project where this is failing for me to test? Looking at your problem description, this looks like it might have to do with the BelongsToMany pivot functionality.
@mikebronner thank you for your answer :)
Composer confirms I'm using version 0.1.15 of the library:
$ composer show GeneaLabs/nova-map-marker-field
name : genealabs/nova-map-marker-field
descrip. : A Laravel Nova field.
keywords : laravel, nova
versions : * 0.1.15
I'll look into creating a basic project, not sure at what speed I can provide you with it though.
Btw: moving the fields in a dedicaded invokable class doesn't change. The Pivot
exists as a model but not as a resource. I'll try exploring the Resource way tomorrow.
@phlisg Thanks for the additional details. Your Nova resources should mirror your models. I would highly recommend making a Nova resource for each model you have, and creating the relationships the same way. That's possibly why its not working, as the Nova resource relies on the underlying model and its relationships to function properly.
So if you are adding a BelongsToMany relationship on your Nova resource that doesn't exist on your model, then I would not expect it to work.
@mikebronner Thank you again for your follow-up
Ah! For instance every Model has a corresponding Resource - except my PivotTables. I just discovered through reading the Nova documentation that one should provide a Fields
class instead of a Resource
class when defining BelongsToMany
fields, not sure I can use a Resource for my Pivot
table
Here's a "UML" of my current implementation:
class \App\Visit
belongsToMany(\App\Stage)
class \App\Stage
belongsToMany(\App\Visit)
class \App\VisitStage extends Pivot
// defining $fillable and $sortable (package `Spatie/EloquentSortable`)
class \App\Nova\Visit
BelongsToMany::make('Stages', 'stages', \App\Nova\Stage::class)
->fields(new StagePivotFields), // these fields are same as my initial comment
class \App\Nova\Stage
BelongsToMany::make('Visits', 'visits', \App\Nova\Visit::class)
->fields(new StagePivotFields),
Thanks for your help
EDIT:
Something worth mentionning: I'm using the Sortable package on my eloquent models, so my BelongsToMany in \App\Visit
& \App\Stage
looks like this:
// \App\Visit
public function stages()
{
return $this->belongsToMany(Stage::class, 'visit_stages')
->withPivot('sort_order')
->using(VisitStages::class)
->orderBy('visit_stages.sort_order');
}
@phlisg Don't treat the pivot model like a pivot table. As soon as you make it a model, its not a pivot table anymore. Treat it like any other model. It should also extend Eloquent\Model, and should have its own Nova resource. And then you don't have BelongsToMany relationships anymore, but rather hasMany relationships and what used to be the pivot is now a model with two belongsTo relationships. Think of it as refactoring out the pivot.
@mikebronner mmmh, thanks for the insight!
I had to create that Pivot
model for the sortable package, as per its documentation https://github.com/optimistdigital/nova-sortable#sorting-belongstomany-relationship-w-pivot-table
I'll need to think how to orchestrate all the packages together haha
@phlisg I think that problem will go away, as you won't have a BelongsToMany relationship anymore. I do not create pivot tables at all anymore, but make them full models instead. Doing that conveys meaning and purpose to what used to be pivot tables, and being a full model provides additional functionality and flexibility. :)