[英]I am POST-ing an array of objects to my API. How can I access the properties of those objects?
I have the following database table:我有以下数据库表:
Table: Locations
Columns: id, lat, lng, user_id
I'm currently POST-ing a markers array the contains 2 JavaScript objects from the front-end to the back-end that have lat and lng properties.我目前正在发布一个标记数组,其中包含从前端到后端的 2 个 JavaScript 对象,这些对象具有 lat 和 lng 属性。 I'm trying to figure out how would I assign the lat and lng properties in my foreach loop.
我想弄清楚如何在 foreach 循环中分配 lat 和 lng 属性。 I just can't figure out how to access them.
我只是不知道如何访问它们。
My POST我的帖子
data: function() {
return {
markers: [{lat: 42, lng: 24}, {lat: 11, lng: 22}]
}
},
methods: {
saveLocations(){
axios.post('/location', {
userId: 1,
markers: this.markers
}).then(response => {
console.log(response);
}).catch((error) => console.log(error));
}
}
PHP PHP
public function newLocation(Request $request){
$markers = $request['markers'];
foreach ($markers as $marker) {
$location = new Location();
$location->user_id = 1;
$location->lat = ;
$location->lng = ;
$location->save();
}
return response()->json([
'message' => 'Successfully added locations!'
], 201);
}
inside your foreach loop use it like this在你的 foreach 循环中像这样使用它
foreach ($markers as $marker) {
$location = new Location();
$location->user_id = 1;
$location->lat = $marker['lat'];
$location->lng = $marker['lng'];
$location->save();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.