-
Notifications
You must be signed in to change notification settings - Fork 305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Format for submitted values #39
Comments
I tried using the output format on input, but that didn't work either. |
Can you show your Please note that we have
Also, note that the JSON output is formatted into GeoJSON and I unfortunately haven't had enough time to work on #23. |
It's all very minimal. My model: <?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;
class Location extends Model
{
use SpatialTrait;
protected $fillable = [
'name',
'location',
];
protected $spatialFields = [
'location',
];
} My controller's creation method is simply: public function create(Request $request)
{
$location = Location::create($request->all());
return response()->json($location);
} What's apparent from what you say is that there is no preset format for the point field type, so I'm assuming I need to expand the point value into lat long for delivery to the client, and map it back from lat & long on input. I was hoping there would be some standardised way to handle that, as there is for strings, numbers and arrays, for example to automatically map I assume pretty much everyone needs to do this kind of processing, so I was a bit surprised that there was no info on how to do it. |
You're absolutely right, there's currently no standardized way to handle input and it's a much needed feature. The current implementation of the package only integrates with Eloquent/MySQL and allows to serialize spatial fields to GeoJSON. Regrettably, until #23 is completed there isn't a straightforward way to deserialize from GeoJSON.
Feel free to submit a PR ;) |
Hi, any update on this? I am trying to do a bulk insert in MySQL using MyModel::insert($myModelInstances[]), where myModelInstances[] is an array of serialized instances new MyModel()->toArray(). The MyModel class in question contains a Point field. As a workaround, I have tried, after converting the instances of MyModel to array, to manually set the field point with $serializedInstance['point'] = "ST_GeomFromText('{$point->toWkt()}')";. But I am getting the same error. If I grab the SQL that outputs the error and run it in MySQL, it inserts without any problems: SQL: insert into Any ideias on how to adress it? Thanks. |
I have found a workaround using: $serializedInstance['point'] = DB::raw("ST_GeomFromText('{$point->toWkt()}')"); |
I have the same issue. Here is what I'm planning on doing. Curious to hear if there is an easier or better way to do it. I have:
which calls
|
Hi guys, just experienced the same issue and after a long day of googling, I found that I forgot to use the i.e. use Illuminate\Database\Eloquent\Model;
use Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait;
class MyModel extends Model
{
use SpatialTrait;
protected $spatialFields = [ 'field', ];
// ...
} That should solve it. |
I have found a way to make the input work, but it doesn't make much sense. I would have written this as a wiki article, but it seems that the wiki is disabled in this repo. Here goes... In a Laravel app, I have a $table->point('location')->spatialIndex(); The problem is that there is no defined input format for such fields - every single example of using point types I've found always defines the point value within the script; it's never a user-submitted value! In the absence of such a format, the most straightforward thing to do is implement public function getLongitudeAttribute(): float
{
return $this->location->getLng();
}
public function getLatitudeAttribute(): float
{
return $this->location->getLat();
}
public function setLongitudeAttribute(float $longitude): void
{
$this->location->setLng($longitude);
}
public function setLatitudeAttribute(float $latitude): void
{
$this->location->setLat($latitude);
} To tick a bunch of other boxes for this, lat & long appear in protected $fillable = [
...
'longitude',
'latitude',
...
]; For the geospatial stuff, it needs to be told which are spatial fields, and we never want the protected $spatialFields = [
'location',
];
protected $hidden = [
'location',
]; Lat & long are added dynamically to the model using protected $appends = [
'longitude',
'latitude',
];
protected $casts = [
'longitude' => 'float',
'latitude' => 'float',
]; That all looks about right, but when I try to create a new record, it fails because the Add the $request->request->add(['location' => new Point($request->input('latitude'), $request->input('longitude'))]); and then I pass in the $location = Location::create(
$request->only(
[
...
'location',
]
)
); While this works, it's really not the "right" way to achieve it; I should not need to inject this conversion manually; it should be implicit when setting latitude & longitude attributes. It also means that I need to do the same thing any time the location needs to be updated, and that's a recipe for bugs. Anyway, so while this works, it seems incredibly convoluted and inelegant - I'd love to see a better way! |
I use a mutator like this:
of course the json is like this:
|
I've created a point type field in my DB on a
location
model, I can set its value explicitly by creating aPoint
instance as per the examples, but what I've been unable to figure out is what format it wants on the input side, with values coming from a client. If I submit something likelocation=Point(51.509865,-0.118092)
in my HTTP params, I end up with an error when I pass it to$location = Location::create($request->all());
:It looks like this needs something like
Point::toWKT()
applied to it, but I can't see how/where I'm meant to apply that automatically.Do I need to handle some separate lat/long string values and convert them to a point on input? Perhaps with a mutator? Is there some input format that "just works"?
For the record (and which I've not seen documented anywhere), the JSON output format is:
The text was updated successfully, but these errors were encountered: