dongyi1777 2019-07-28 13:55
浏览 124
已采纳

Laravel更新表单只显示一个id

I have a page that displays records from the database and two buttons with the addition of a new record and editing using the bootstrap modal in Laravel 5.8. When I add a new record to the database, everything works. The problem is when editing a record it only changes the first record because it does not transfer the remaining id it always shows only the first id. As I display the records with each button edit, I display the id of the record and everything shows correctly but after pressing the button edit does not provide the id. In the form I use the following code. source: [a link]https://github.com/page4me/transport


@foreach($cars as $car)
               @if(($car->status)==1)

                 <tr>
                   <td class="text-center">{{$i++}}</td>
                   <td>{{$car->rodzaj_poj}}<br /><strong> {{$car->marka}} </strong></td>
                   <td class="text-center">{{$car->nr_rej}}</td>
                   <td class="text-center">{{$car->nr_vin}}</td>
                   <td class="text-center">{{$car->dmc}} kg</td>
                   <td class="text-center">{{$car->wlasnosc}}</td>
                   <td class="text-center">wprowadzono <br />{{$car->data_wpr}} r.</td>
                   <td class="text-center" colspan="2">
                   {{$cid =$car->id}}
                    <button data-toggle="modal" data-idcar="{{$car->id}}" data-nr_rej="{{$car->nr_rej}}"  data-marka="{{$car->marka}}" data-nr_vin="{{$car->nr_vin}}" data-wlasnosc="{{$car->wlasnosc}}" data-data_wpr="{{$car->data_wpr}}" data-dmc="{{$car->dmc}}" data-rodzaj_poj="{{$car->rodzaj_poj}}" data-target="#editModal" role="button" class="btn btn-success btn-sm" alt="Edycja" ><i class="fa fa-edit"></i></button>

                    <a href="#" role="button" class="btn btn-danger btn-sm">Wycofaj</a>

                                      <!-- edit modal -->
                                <!-- Modal -->
                                  <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
                                    <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
                                      <div class="modal-content text-left">
                                        <div class="modal-header  bg-success">
                                          <div class="card-header bg-success text-light" >
                                             Edycja pojazdu przedsiębiorcy o  -
                                               <span style="color:#000;font-size:15px;"> Nr licencji / zezwolenia:
                                                 @foreach($dok as $dk)
                                                   {{ $dk->nr_dok }}

                                               </span><span style="color: #fff;font-size:15px;">wydano dn. {{ $dk->data_wyd}}   r.</span>

                                                 @endforeach
                                            </div>
                                          <button type="button" class="close text-light" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">&times;</span>
                                          </button>
                                        </div>
                                        <div class="modal-body">
                                           ID:  {{$cid =$car->id}}
                                            <!-- Form edit car -->
                                                <form method="post" action="{{ route('pojazdy.update', $cid ) }}">
                                              <div class="row">
                                                    @csrf
                                                      @method('PATCH')
                                                 <div class="col-md-12 form-group">
                                                    <label for="nr_rej"><strong>Numer rejestracyjny:</strong></label>
                                                    <input type="text" class="form-control" name="nr_rej" id="nr_rej" value="{{$car->nr_rej}}"/>
                                                </div>
                                               </div>

It always displays only the first id even though it is displayed in the foreach loop. next to buttons edit correctly shows id and when using in action = "{{route ('vehicle.update', $ car-> id)}}" there is already a problem

  • 写回答

1条回答 默认 最新

  • dongxian6741 2019-07-28 14:25
    关注

    The problem you are having is that you are looping on $cars and setting multiple elements within the dom to the same id. Within that foreach loop, you have several items that will be overwritten many times.

    For example:

    //This SAME id will appear every time you have a new car loop
    <div class="modal fade" id="editModal"/>
    
    //This nr_rej will ONLY be the LAST nr-rej in the loop.  It will overwrite every one before it.
    <input type="text" class="form-control" name="nr_rej" id="nr_rej" value="{{$car->nr_rej}}"/> 
    

    You can fix this in several ways. You can add arrays to the values, you can add an actual car id to the dom element ids (id=nr_rej-{{$car->id}}), etc. But typically, this is easiest if you load the modal via an ajax call - this way you don't have to stick modal code on the screen for every loop - just one.

    Hope this helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 关于#html5#的问题:H5页面用户手机返回的时候跳转到指定页面例如(语言-javascript)
  • ¥15 无法使用此凭据登录,因为你的域不可用,如何解决?(标签-Windows)
  • ¥15 yolov9的训练时间
  • ¥15 二叉树遍历没有报错但无法正常运行
  • ¥15 在linux系统下vscode运行robocup3d上场球员报错
  • ¥15 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动