douchuitang0331 2016-03-08 11:01
浏览 209
已采纳

Laravel 5 Update存在于数据库中

I'm trying to update an existing row in my table using a form in a bootstrap modal. But nothing changes when I attempt it. I have an html table with a list of cars and in each row a button "Change Status" that opens the bootstrap modal with the id="myModal_{{ $car->id }}" and with the form inside it to edit the status of a car in repair.

Here's my modal:

<!-- Modal -->
<div class="modal fade" id="myModal_{{ $car->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Breyta stöðu bíls</h4>
      </div>
      <div class="modal-body">
        <div class="form-group">
          <h3>Breyta stöðu fyrir {{ $car->LicencePlate }}</h3>
        </div>
            {!! Form::open(['url' => 'create']) !!}

              <div class="form-group">
                {!! Form::label('Status', 'Status: ') !!}
                {!! Form::select('Status', [
                                  null => ' -- Veldu Stöðu bíls -- ',
                                  'Í Biðröð' => 'Í Biðröð',
                                  'Í Viðgerð' => 'Í Viðgerð',
                                  'Tilbúinn' => 'Tilbúinn'
                                  ], null, ['class' => 'form-control']) !!}
              </div>
              <div class="form-group">

              </div>

      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        {!! Form::submit('Skrá stöðu', ['class' => 'btn btn-primary']) !!}
      </div>
      {!! Form::close() !!}
    </div>
  </div>
</div>

Update method in CarController:

public function CarEdit($id, Requests\CreateCarsRequest $request)
{
  $edit = Car::update($request->all());
  return redirect::back();
}

My Route to that method:

Route::post('/dashboard', 'CarController@CarEdit');
  • 写回答

2条回答 默认 最新

  • dongxuxian1123 2016-03-08 11:39
    关注

    I was able to fix this with the help of my colleagues. What I did was I created a new requet called UpdateCarRequest where the LicencePlate field was not unique. And edited my method like so:

    public function CarEdit(Requests\UpdateCarRequest $request)
    {
      $edit = Car::where('LicencePlate', '=', $request->LicencePlate)->first();
      $edit->update($request->all());
      return redirect('/dashboard');
    }
    

    I also added a hidden field in the form with the value $car->LicencePlate.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误