我有以下数据库表:
Table: Locations
Columns: id, lat, lng, user_id
我目前正在发布一个标记数组,该数组包含从前端到后端的两个具有lat和lng属性的JavaScript对象。我试图弄清楚如何在我的foreach循环中分配lat和lng属性。我只是不知道如何访问它们。
我的帖子
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
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);
}
答案 0 :(得分:1)
在您的foreach循环内像这样使用它
foreach ($markers as $marker) {
$location = new Location();
$location->user_id = 1;
$location->lat = $marker['lat'];
$location->lng = $marker['lng'];
$location->save();
}