dongxiang5879 2019-01-26 11:41
浏览 29
已采纳

使用laravel更新模式中的记录,而不是保存

I'm trying to update records in the mysql database with laravel through a bootstrap modal. Here's how I have collected data from the table of records,

<button class="site-button" data-toggle="modal" data-target=".edit-booking"  data-bookingid="{{ $records->id }}" data-firstname="{{ $records->firstname }}" data-lastname="{{ $records->lastname }}" data-roomtype="{{ $records->roomtype }}" data-checkin="{{ $records->checkin }}" data-checkout="{{ $records->checkout }}" data-adults="{{ $records->adults }}" data-children="{{ $records->children }}" data-email="{{ $records->email }}" data-phone="{{ $records->phone }}" data-payment="{{ $records->payment }}" data-additionals="{{ $records->additionals }}" data-address="{{ $records->address }}"><span class="fa fa-edit"> Edit</span></button> / <button class="site-button"><span class="fa fa-trash"> Delete</span></button>

My modal for editing,

<div class="modal-content">
        <div class="modal-header bg-secondry">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title text-black">Edit Booking</h4>
        </div>
        <div class="modal-body edit-modal-body">
          <div class="section-content">
          <!-- CONTACT FORM-->
          <div class="wt-box">
            <form class="contact-form" id="edit-form" action="{{route('booking.update',$records->id)}}" name="edit-form" method="post"> {{method_field('patch')}} {{csrf_field()}}
              <div class="contact-one p-a40 p-r150">
                <div class="form-group">
                  <input name="editbookingid" id="editbookingid" type="hidden" class="form-control" value="">
                </div>
                <div class="form-group">
                  <label for="edit_firstname">First Name</label>
                  <input name="edit_firstname" id="edit_firstname" type="text" required class="form-control">
                </div>
                <div class="form-group">
                  <label for="edit_lastname">Last Name</label>
                  <input name="edit_lastname" type="text" id="edit_lastname" required class="form-control">
                </div>
                <div class="form-group">
                  <label for="edit_email">Email</label>
                  <input name="edit_email" type="email" id="edit_email" class="form-control" required>
                </div>
                <div class="form-group">
                  <label for="edit_phone">Phone</label>
                  <input name="edit_phone" type="phone" id="edit_phone" class="form-control" required>
                </div>
                <div class="form-group">
                <label for="edit_address">Address</label>
                  <textarea name="edit_address" id="edit_address" rows="3" class="form-control " required></textarea>
                </div>
                <div class="form-group">
                  <label for="edit_roomtype">Room type</label>
                  <input name="edit_roomtype" id="edit_roomtype" type="text" class="form-control" required>
                </div>
                <div class="two-columns-row">
                  <div class="form-group two-columns">
                    <label for="edit_adults">Adults</label>
                    <input type="text" name="edit_adults" id="edit_adults" class="form-control" required>
                  </div>
                  <div class="form-group two-columns">
                    <label for="edit_children">Children</label>
                    <input type="text" name="edit_children" id="edit_children" class="form-control" required>
                  </div>
                </div>
                <div class="two-columns-row">
                  <div class="form-group two-columns">
                    <label for="edit_checkin">Check-in Date</label>
                    <input type="date" name="edit_checkin" id="edit_checkin" class="form-control" required>
                  </div>
                  <div class="form-group two-columns">
                    <label for="edit_check-out">Check-out Date</label>
                    <input type="date" name="edit_checkout" id="edit_checkout" class="form-control" required>
                  </div>
                </div>
                <div class="form-group">
                  <label for="edit_payment">Payment Method</label>
                  <input name="edit_payment" type="text" id="edit_payment" required class="form-control">
                </div>
                <div class="form-group">
                  <label for="edit_additionals">Additional Notes</label>
                  <textarea name="edit_additionals" rows="3" id="edit_additionals" class="form-control"></textarea>
                </div>
                <div class="modal-footer">
                  <button type="button" class="site-button black radius-no text-uppercase" data-dismiss="modal">Close</button>
                  <button type="submit" class="site-button black radius-no text-uppercase" name="edit_submit">Save Changes</button>
                </div>
              </div>
            </form>
          </div>
        </div>
        </div>
      </div>

Script used to fill the modal,

<script>
  /* For filling data in the edit modal */
  $('.edit-booking').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget)
    var recordid = button.data('bookingid')
    var firstname = button.data('firstname')
    var lastname = button.data('lastname')
    var roomtype = button.data('roomtype')
    var checkin = button.data('checkin') 
    var checkout = button.data('checkout')
    var adults = button.data('adults')
    var children = button.data('children')
    var email = button.data('email')
    var phone = button.data('phone')
    var payment = button.data('payment')
    var additionals = button.data('additionals')
    var address = button.data('address') 
    var modal = $(this)

    modal.find('.edit-modal-body #editbookingid').val(recordid);
    modal.find('.edit-modal-body #edit_firstname').val(firstname);
    modal.find('.edit-modal-body #edit_lastname').val(lastname);
    modal.find('.edit-modal-body #edit_roomtype').val(roomtype);
    modal.find('.edit-modal-body #edit_checkin').val(checkin);
    modal.find('.edit-modal-body #edit_checkout').val(checkout);
    modal.find('.edit-modal-body #edit_adults').val(adults);
    modal.find('.edit-modal-body #edit_children').val(children);
    modal.find('.edit-modal-body #edit_email').val(email);
    modal.find('.edit-modal-body #edit_phone').val(phone);
    modal.find('.edit-modal-body #edit_payment').val(payment);
    modal.find('.edit-modal-body #edit_additionals').val(additionals);
    modal.find('.edit-modal-body #edit_address').val(address);
  })
</script>

This is my update() in the controller,

public function update(Request $request, $id)
    {
        $an_update = Bookings::findOrFail($id);
        $an_update->update($request->all());
        return back();
    }

and this is the model,

class Bookings extends Model
{
    protected $fillable = ['firstname', 'lastname', 'email', 'phone', 'address', 'adults', 'children', 'checkin', 'checkout', 'additionals', 'roomtype', 'payment'];
}

After filling all the changes in the form, if I click "save changes", the page reloads but the record is not updated at all.

  • 写回答

2条回答 默认 最新

  • dousi1994 2019-01-26 12:36
    关注

    Looks to me the way you are assigning the values from the request is what is wonky. I haven't tested this personally yet, in a bit of a rush. Will come back and update this answer in a few hours. In the mean time, give this a try.

    $an_update = Bookings::findOrFail($id);
    $an_update->firstName = $request->input('customer_license_plate');
    $ticket->save();
    
    return back();
    

    Do that for all your records fields, remember that back() will redirect the user back to the page they came from regardless of a success or fail. You may wish to consider additional logic to redirect to another page upon success (like a listing screen for example).

    Reference: https://laravel.com/docs/5.5/eloquent#updates

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

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组