简体   繁体   中英

How can I insert the data into the database after hitting submit?

How can I insert the data into the database after hitting submit, I have no idea how to connect the apartment street to the apartment 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>

Try this way.

@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

and in controller fetch as it $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'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM