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

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

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行