How to update geospatial data via laravel 8 mongodb - laravel

I'm trying to update geospatial data in mongodb using laravel version 8, while login i've to update long,lat in geospatial coordinates in mongo database, data how it stored in the database is shown below, here "point" is field name of geospatial coordinates.
point:Object
type:"Point"
coordinates: Array
0:122.00084
1:37.4219983
This is how it stored in database
i'm passing the value in update query as follows
$date = Carbon::now();
$timestamp = $date->getPreciseTimestamp(3);
$updateData = ['user_token' => $token,
'update_at' => $timestamp,
'point' => ['type' => 'Point', 'coordinates' => [$long, $lat]]
];
$q = Post::where('user_id', $userID)->update($updateData);
it throws an error "exception":
"MongoDB\Driver\Exception\BulkWriteException",
{
"message": "Can't extract geo keys: { _id: \"261058c2f2576b4f72365d3ee1d5a03d\", user_id: \"7c2d166e170a6d8457ca16424892b05b\", name: \"qwer\", profile_pic: \"\", email: \"mailid#mail.com\", password: \"e10adc3949b234a59a2134bbe56e0572134f20f883e\", city: \"ind\", state: \"IN\", zip: \"600000\", country: \"India\", user_token: \"asdfadsf\", point: { type: \"Point\", coordinates: [ \"-122.084\", \"37.4219983\" ] }, is_active: true, verifiy_key: \"168d224a36785f00e28e232068qerweadcac8f1fsdfcbeade5f643cbd2e683548fd069200423749b\", email_status: true, is_not",
"exception": "MongoDB\\Driver\\Exception\\BulkWriteException",
"file": "/var/www/html/ce5/vendor/mongodb/mongodb/src/Operation/Update.php",
"line": 200,
}

Related

Using Http::post to get data from API

I tried to get data from API, when in Postman it's working fine but when i tried it in laravel it return an error
How do i get that data to laravel?
This is from my controller :
$response = Http::post('https://url.id/api/tryout/check', [
'category_id' => 0,
'platform' => 'platform',
'is_unique' => false,
'chapters' => [
'id' => 3,
'name' => "Aljabar",
'count' => 10,
],
]);
return $response;
But the code above will return an error :
Trying to access array offset on value of type int
you are passing your chapters data as array of object in your POSTMAN,
"chapters": [
{
"id": 1,
..
..
},
{
"id": 3,
..
..
}
]
then its a plain object in your Laravel HTTP request, which throwing an error because you are looping through id, name and count and not through an object containing those key which your loop expects to have
"chapters": {
"id": 3,
..
..
}
you need this part to be a list of objects
'chapters' => [
'id' => 3,
..
],
should be
'chapters' => [
[
'id' => 3,
..
]
],
You are sending 'category_id' => 0 I don’t believe that it exists in your database. You have to change it to 'category_id' => 1
And also you have to follow this answer too.

store results from method in data form

I am going to preface this with I have NOT done this in a LONG time and my mind is mud. So no picking on me just remind me what I am doing wrong or not remembering.
NOTE: This is VueJS 3 / Tailwind 3 inside a Laravel 9 Jetstream Project
I have a method...
locatorButtonPressed() {
navigator.geolocation.getCurrentPosition(
position => {
console.log(position.coords.latitude);
console.log(position.coords.longitude);
},
error => {
console.log(error.message);
},
)
},
and I have a form data
data() {
return {
form: this.$inertia.form({
name: '',
email: '',
password: '',
password_confirmation: '',
birthdate: '',
user_latitude: '',
user_longitude: '',
user_city: '',
user_region: '',
user_country: '',
location: '',
terms: false,
})
}
}
I want to store position.coords.latitude and position.coords.longitude from the method in the form Data under user_latitude and user_longitude respectfully.
What am I forgetting???
The data properties can be accessed via this.PROPERTY_NAME. For example, to set form.user_latitude, use this.form.user_latitude = newValue.
export default {
methods: {
locatorButtonPressed() {
navigator.geolocation.getCurrentPosition(
position => {
this.form.user_latitude = position.coords.latitude;
this.form.user_longitude = position.coords.longitude;
})
},
}
}
demo

GraphQL: UnionType with Scalar and InputObjectType

I have a database storing some videogames. Each of them has three release dates: one for Europe, another for America and a last one for Japan.
I would like to allow users of my GraphQL API to search for games according to these dates. For exemple, in order to get name and release date of games released in Europe and America but not released in Japan, we can write this query:
{
games(has_been_released: { eur: true, usa: true, jap: false }) {
data {
name
release_date
}
}
}
Therefore, if we want to fetch games released in all regions, the query will be this one:
{
games(has_been_released: { eur: true, usa: true, jap: true}) {
data {
name
release_date
}
}
}
When all booleans have the same value, I would like to simplify the query so that we could write the below instead:
{
games(has_been_released: true) {
data {
name
release_date
}
}
}
In order to do that, I've tried this type definition for field has_been_released (with graphql-php):
<?php
$regionalizedBooleanInput = new InputObjectType([
'name' => 'RegionalizedBooleanInput',
'description' => 'An object with properties "eur", "usa" and "jap" as booleans',
'fields' => [
'eur' => ['type' => Type::boolean(), 'defaultValue' => null],
'usa' => ['type' => Type::boolean(), 'defaultValue' => null],
'jap' => ['type' => Type::boolean(), 'defaultValue' => null],
]
]);
$booleanOrRegionalizedBooleanInputType = new UnionType([
'name' => 'BooleanOrRegionalizedBooleanInput',
'description' => 'A boolean that can be different according to regions',
'types' => [
Type::boolean(),
$regionalizedBooleanInput,
],
'resolveType' => function($value) use ($regionalizedBooleanInput) {
if ($value->type === 'boolean') {
return Type::boolean();
}
return $regionalizedBooleanInput;
},
]);
But when I do that, GraphiQL throws this error:
Error: Introspection must provide object type for possibleTypes.
at invariant (chrome-extension://fkkiamalmpiidkljmicmjfbieiclmeij/dist/chromeiql.js:14605:11)
at getObjectType (chrome-extension://fkkiamalmpiidkljmicmjfbieiclmeij/dist/chromeiql.js:72489:80)
at Array.map ()
at buildUnionDef (chrome-extension://fkkiamalmpiidkljmicmjfbieiclmeij/dist/chromeiql.js:72566:47)
at buildType (chrome-extension://fkkiamalmpiidkljmicmjfbieiclmeij/dist/chromeiql.js:725
So I assume something is wrong with my types definition, but I don't get why. Any ideas?
tl;dr: I would like to have a GraphQL field which can accept a scalar value or an InputObject with this scalar type as fields. Is it even possible?
Thanks in advance!
GraphQL currently doesn't support polymorphic input type or Union in input .
https://github.com/graphql/graphql-spec/issues/488
One solution could be all_regions to part of input schema but not mandatory
input Regions{
eur: Boolean
usa: Boolean
jpa: Boolean
all_regions: Boolean
}
{
games(has_been_released: { all_regions: true}) {
data {
name
release_date
}
}
}

Using data from API to populate Quasar Tree - trouble sending handler function

I'm using Quasar to create a tree:
<q-tree v-if="tree"
:nodes="tree"
node-key="label"
default-expand-all
/>
If I manually enter the node data and use the handler node property to apply a click function to my nodes, it works great:
data () {
return {
tree: [ { id: 7, label: 'Master Stateroom', icon: 'crop_3_2', children: [ { id: 4, label: 'Center Bilge Compartment', icon: 'crop_3_2', handler: (node) => this.goCompartment(node) } ], handler: (node) => this.goRoom(node) } ]
}
},
and my basic, testing methods:
methods: {
goRoom (room) {
alert(room.id)
},
goCompartment (compartment) {
alert(compartment.label)
}
}
All is good, however, I want to load in my nodes from a database and I'm having trouble getting the handler function to work - it's just a string.
An excerpt from my API:
return [ 'id' => $this->id, 'label' => $this->name, 'icon' => 'crop_3_2', 'children' => $children, 'handler' => '(node) => this.goRoom(node)' ];
I know I don't want just a string there, it needs to be evaluated as a function. If I review the data in Vue Devtools, I see the handler passed from my API is just a string and the hardcoded, local version, shows up as a function.
Anyone have any tips for passing that correctly from the API to the vue component?

Laravel: Unexpected Behavior of Date in JSON Response

I am making a web service in Laravel which is returning JSON.
I have created an Account model like so:
class Account extends Eloquent {
// The database table used by the model.
// (If not defined then lowercase and plural of class name is consider as a table name)
protected $table = "account";
// define which column can be mass assign
protected $fillable = array("user_id", "account_group_id", "generated_by", "image", "name",
"address", "zip", "area_id", "mobile", "email", "phone", "fax",
"website", "pan", "cst", "tin", "ecc", "iesc", "transport",
"other", "outstanding", "cform", "status", "mitp");
// To prevent column from mass assignment.
protected $guarded = array('id');
// Change Variable for CREATED_AT and UPDATED_AT
const CREATED_AT = 'itp';
const UPDATED_AT = 'utp';
}
I am fetching fields from Account using user_id and returning JSON via Response::json() in my controller
$accountData = Account::select('name', 'status', 'id', 'user_id', 'utp')->where('user_id', Auth::id())->first();
$return = array(
'result' => 'success',
'msg' => 'Login Successfully.',
'data' => $accountData
);
return Response::json($return);
In this, utp behaves as expected and returns a date as a string:
{
"result": "success",
"msg": "Login Successfully.",
"data": {
"name": "Demo",
"status": 0,
"id": 143,
"user_id": 207,
"utp": "2015-07-01 18:38:01"
}
}
However if I take each value separately from the account model like so:
$return = array(
'result' => 'success',
'msg' => 'Login Successfully.',
'data' => $accountData['user_id'],
'account_id' => $accountData['id'],
'utp' => $accountData['utp'],
'usertype' => 'account',
'status' => $accountData['status']
);
Then this gives some unexpected behavior from utp
{
"result": "success",
"msg": "Login Successfully.",
"data": 207,
"account_id": 143,
"utp": {
"date": "2015-07-01 18:38:01",
"timezone_type": 3,
"timezone": "Asia\\/Kolkata"
},
"usertype": "account",
"status": 0
}
Why does this happen with my timestamp field?
Because utp is a Carbon\Carbon instance. Model::toJson (actually Model::toArray, but both are used) handles that usually, and serializes a date to it's usual ISO3601-ish format
For expected behavior, you need to format the Carbon instance.
"utp" => $accountData['utp']->format("Y-m-d H:i:s"),
Alternatively, cast it to a string
"utp" => (string) $accountData['utp'],

Resources