laravel updateorCreate method
我的方法中有以下代码,我通过ajax发送到控制器方法:
$newUser = \App\UserInfo::updateOrCreate([
'user_id' => Auth::user()->id,
'about' => $request->get('about'),
'sec_email' => $request->get('sec_email'),
'gender' => $request->get("gender"),
'country' => $request->get('country'),
'dob' => $request->get('dob'),
'address' => $request->get('address'),
'mobile' => $request->get('cell_no')
]);
给我 :dd($request->all())
array:8 [
"_token" => "fHeEPfTvgMD3FpIBmmc6DmKXFaiuWKZEiOhg6twQ"
"about" => "Some about me."
"sec_email" => "example@gmail.com"
"country" => "Priority highest"
"gender" => "male"
"dob" => "12/12/1990"
"address" => "Some address"
"cell_no" => "234234234"
]
这是完美的。
Jquery 代码 :
$('#submit-editProfile-form').on('click', function() {
var profileEditForm = $("#edit-user-profile");
var formData = $('#edit-user-profile').serialize();
profileEditForm.on('submit', function(e){
e.preventDefault();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url:'/freelance/edit-userProfile-info',
type:'POST',
data:formData,
error: function (data) {
console.log('Error');
}
});
}).submit();
});
现在的问题是,我的表中有一条记录,但是上面的代码创建了另一个记录,第二个是它在每个按钮单击(请求)时创建乘以两条记录。