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条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥20 怎么将vs中的程序同步到sql中的表中
      • ¥15 用flask框架接收前端上传的文本文件后怎么读取该文件
      • ¥15 有没有嵌入式ubuntu的朋友
      • ¥50 diddler Burp Suite
      • ¥20 关于UE5.1的问题,求专家解答!
      • ¥30 关于#爬虫#的问题:百度为您找到相关结果约100,000,000个 但只展现70页 有没有可能提取出来全部的搜索结果
      • ¥40 IIS部署API,弄了几天没弄出来,希望带佬远程帮我弄一下,有偿
      • ¥15 关于#c##的问题:winform中利用delegate更新ui的顺序错误
      • ¥15 有没有985信息安全专业的同学说一下你们都学什么。。
      • ¥15 qt5.6.1读取oracle8中文数据乱码