首页 > 解决方案 > 使用foreach在数据库laravel javascript中双重输入每个输入类型的文本

问题描述

我的数据库中有这个。

人 | 地点

学生1 旧金山

学生 2 旧金山

纽约学生1

学生2纽约

我的控制器有问题。这是代码:

foreach((array)$request->stud as $person){
  foreach((array)$request->studlocation as $location){
    $hm = new HouseMate;

    $hmid = rand();
    $hm->hmid=$hmid;
    $hm->rId_fk=$renterid;
    $hm->person=$person;  

    $hm->location=$location;
    $hm->save();
  }
  $hm->delete();
}

在我看来:

$(document).ready(function() {
  $("#dropdown").change(function() {
    var selVal = $(this).val();
    $("#textboxDiv").html('');
    $("#schoolloc").html('');

    if(selVal > 0) {
      for(var i = 1; i<= selVal; i++) {
        $("#textboxDiv").append('<input type="hidden" value="student'+i+'" name="stud[]" />');
        $("#schoolloc").append('<input type="text" name="studlocation[]" id="studlocation'+i+'" />');
        var input = document.getElementById('studlocation'+i+'');
        var autocomplete = new google.maps.places.Autocomplete(input);

        autocomplete.addListener('place_changed', function () {
          var place = autocomplete.getPlace();
        });
      }
    }
  });

标签: javascriptphpdatabaselaravelautocomplete

解决方案


这是你想要的吗?

代码 php:

$students = (array)$request->stud;
$locations = (array)$request->studlocation;
for ($i = 0; $i < count($studens); i++){
    $hm = new HouseMate;

    $hmid = rand();
    $hm->hmid=$hmid;
    $hm->rId_fk=$renterid;
    $hm->person=$students[i];  

    $hm->location=locations[i];
    $hm->save();
    $hm->delete();
}

推荐阅读