为什么当我更新特定记录时添加新记录?
当我更新特定的记录时,该记录不会更新,但是添加了新记录,并且该记录不会更新。
我使用updateOrcreate()
,因为我使用一种模态添加和更新,然后使用AJAX将数据传递给控制器。 只是这样,您知道我有一个外键,如果这可能有帮助,那就是航空公司。
ajax:
$('body').on('click', '#btn-save', function (event) {
var id = $("#id").val();
var flightDesignator = $("#flightDesignator").val();
var departureFrom = $("#departureFrom").val();
var arriveTo = $("#arriveTo").val();
var departureTime = $("#departureTime").val();
var ArrivalTime = $("#ArrivalTime").val();
var airlineId = $("#airlineId").val();
$("#btn-save").html('Please Wait...');
$("#btn-save"). attr("disabled", true);
$.ajax({
type:"POST",
url: "{{ url('admin/add-update-flights') }}",
data: {
id:id,
flightDesignator:flightDesignator,
departureFrom:departureFrom,
arriveTo:arriveTo,
departureTime:departureTime,
ArrivalTime:ArrivalTime,
airlineId:airlineId,
},
dataType: 'json',
success: function(res){
window.location.reload();
$("#btn-save").html('Submit');
$("#btn-save"). attr("disabled", false);
Swal.fire(
'Good job!',
'You clicked the button!',
'success'
)
}
});
});
控制器:
public function store(Request $request)
{
$Flight = Flight::updateOrCreate(
[
'FlightId' => $request->id
],
[
'flightDesignator' => $request->flightDesignator,
'departureFrom' => $request->departureFrom,
'arriveTo' => $request->arriveTo,
'departureTime' => $request->departureTime,
'ArrivalTime' => $request->ArrivalTime,
'airlineId' => $request->airlineId,
]);
return response()->json(['success' => true]);
}
public function edit(Request $request)
{
$where = array('FlightId' => $request->id);
$flight = Flight::where($where)->first();
return response()->json($flight);
}
When I update a specific record, the record is not updated, but it adds a new record and the record does not update.
I use updateOrCreate()
because I used one modal to add and update and I passed the data to the Controller using ajax.
Just so you know I have a foreign key and it's airlineId if that might help.
ajax:
$('body').on('click', '#btn-save', function (event) {
var id = $("#id").val();
var flightDesignator = $("#flightDesignator").val();
var departureFrom = $("#departureFrom").val();
var arriveTo = $("#arriveTo").val();
var departureTime = $("#departureTime").val();
var ArrivalTime = $("#ArrivalTime").val();
var airlineId = $("#airlineId").val();
$("#btn-save").html('Please Wait...');
$("#btn-save"). attr("disabled", true);
$.ajax({
type:"POST",
url: "{{ url('admin/add-update-flights') }}",
data: {
id:id,
flightDesignator:flightDesignator,
departureFrom:departureFrom,
arriveTo:arriveTo,
departureTime:departureTime,
ArrivalTime:ArrivalTime,
airlineId:airlineId,
},
dataType: 'json',
success: function(res){
window.location.reload();
$("#btn-save").html('Submit');
$("#btn-save"). attr("disabled", false);
Swal.fire(
'Good job!',
'You clicked the button!',
'success'
)
}
});
});
controller:
public function store(Request $request)
{
$Flight = Flight::updateOrCreate(
[
'FlightId' => $request->id
],
[
'flightDesignator' => $request->flightDesignator,
'departureFrom' => $request->departureFrom,
'arriveTo' => $request->arriveTo,
'departureTime' => $request->departureTime,
'ArrivalTime' => $request->ArrivalTime,
'airlineId' => $request->airlineId,
]);
return response()->json(['success' => true]);
}
public function edit(Request $request)
{
$where = array('FlightId' => $request->id);
$flight = Flight::where($where)->first();
return response()->json($flight);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
轻松修复
easy fix
这里首先检查$ request-> id的值。
如果值为空的updateorcreate()将创建记录,而不是更新现有记录。
Here first check that the value of $request->id.
if the value is empty updateOrCreate() will create a record instead of updating the existing record.