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 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 YOLOv8obb获取边框坐标时报错AttributeError: 'NoneType' object has no attribute 'xywhr'
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开