我正在尝试通过引导模式使用laravel更新mysql数据库中的记录。这是我从记录表中收集数据的方式,
<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>
我的编辑模式,
<div class="modal-content">
<div class="modal-header bg-secondry">
<button type="button" class="close" data-dismiss="modal">×</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>
/* 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>
这是我在控制器中的update(),
public function update(Request $request, $id)
{
$an_update = Bookings::findOrFail($id);
$an_update->update($request->all());
return back();
}
这是模型,
class Bookings extends Model
{
protected $fillable = ['firstname', 'lastname', 'email', 'phone', 'address', 'adults', 'children', 'checkin', 'checkout', 'additionals', 'roomtype', 'payment'];
}
填写所有更改后,如果单击“保存更改”,页面会重新加载,但记录根本不会更新。
答案 0 :(得分:0)
$ id是从route参数分配的,但是在打开模态之前,已在表单操作中定义了您的路由。打开模式后,您似乎没有在任何地方更新路线的预订ID。
这意味着无论该值是多少,您始终在更新ID为$records->id
的预订。
答案 1 :(得分:-1)
在我看来,您从请求分配值的方式很奇怪。匆匆忙忙,我还没有亲自测试过。会在几个小时后回来更新此答案。同时,尝试一下。
$an_update = Bookings::findOrFail($id);
$an_update->firstName = $request->input('customer_license_plate');
$ticket->save();
return back();
对所有记录字段执行此操作,请记住,无论成功或失败,back()都会将用户重定向回他们来自的页面。您可能希望考虑在成功后重定向到另一个页面的其他逻辑(例如,列表屏幕)。