点击提交后如何将数据插入数据库,我不知道如何将公寓街道连接到公寓ID。
<tbody>
@foreach($apartments as $apartment)
<tr>
<input type="hidden" name="id[]" value="{{$apartment->id}}" id="checkApt" />
<td><input type="checkbox" name="apt[]" value="{{$apartment->id}}"id="checkApt"></td>
<td><input type="text" name="street[]" value="{{ $apartment->street }}" id="checkApt"></td>
<td><input type="text" name="aptnumber[]" value="{{ $apartment->apartment_number }}" id="checkApt"> </td>
<td><input type="text" name="price[]" value="{{ number_format($apartment->price, 2) }}"id="checkApt"></td>
<td><input type="text" name="bedrooms[]" value="{{$apartment->bedrooms}}" id="checkApt"></td>
<td><input type="text" name="bathrooms[]" value="{{$apartment->bathrooms}}"id="checkApt"></td>
<td><input type="text" name="date[]" value="{{ date('M d, Y', strtotime($apartment->created_at))}}"id="checkApt"></td>
<td><input type="text" name="area[]" value="{{ $apartment->neighborhood->neighborhood ?? '' }}"id="checkApt"></td>
</tr>
@endforeach
</tbody>
答案 0 :(得分:-1)
尝试这种方式。
@foreach($apartments as $apartment)
<tr>
<input type="hidden" name="data[{{$apartment->id}}]['id']" value="{{$apartment->id}}" id="checkApt" />
<td><input type="checkbox" name="data[{{$apartment->id}}]['apt']" value="{{$apartment->id}}"id="checkApt"></td>
<td><input type="text" name="data[{{$apartment->id}}]['street']" value="{{ $apartment->street }}" id="checkApt"></td>
<td><input type="text" name="data[{{$apartment->id}}]['aptnumber']" value="{{ $apartment->apartment_number }}" id="checkApt"> </td>
<td><input type="text" name="data[{{$apartment->id}}]['price']" value="{{ number_format($apartment->price, 2) }}"id="checkApt"></td>
<td><input type="text" name="data[{{$apartment->id}}]['bedrooms']" value="{{$apartment->bedrooms}}" id="checkApt"></td>
<td><input type="text" name="data[{{$apartment->id}}]['bathrooms']" value="{{$apartment->bathrooms}}"id="checkApt"></td>
<td><input type="text" name="data[{{$apartment->id}}]['date']" value="{{ date('M d, Y', strtotime($apartment->created_at))}}"id="checkApt"></td>
<td><input type="text" name="data[{{$apartment->id}}]['area']" value="{{ $apartment->neighborhood->neighborhood ?? '' }}"id="checkApt"></td>
</tr>
@endforeach
并在控制器中按$request->data
的方式获取
foreach($request->data as $val){
echo 'id '.$val['id'];
echo 'apt '.$val['apt'];
echo 'street '.$val['street'];
echo 'aptnumber '.$val['aptnumber'];
echo 'price '.$val['price'];
echo 'bedrooms '.$val['bedrooms'];
echo 'bathrooms '.$val['bathrooms'];
echo 'date '.$val['date'];
echo 'area '.$val['area'];
}