Hi All
I'm trying to update a record using Ajax and being new to Ajax & laravel i'm confused about the result of the query. I'm trying to update a record but when i make the Ajax call, its trying to insert a new record into the DB.
Please could someone point me in the right direction as to where i'm going wrong?
Error:
SQLSTATE[HY000]: General error: 1364 Field 'lname' doesn't have a default value (SQL: insert into `users` (`id`, `fname`, `email`,
Controller:
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$dealerApp = \App\User::updateOrCreate(
['id' => $request->dealerApp_id],
['fname' => $request->fname, 'email' => $request->email]
);
return Response::json($dealerApp);
}
/**
* Show the form for editing the specified resource.
*
* @param \App\User $User
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$where = array('id' => $id);
$dealerApp = \App\User::where($where)->first();
return Response::json($dealerApp);
}
Script in view file:
$(document).ready(function () {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
/* When user click add user button */
// $('#create-new-user').click(function () {
// $('#btn-save').val("create-user");
// $('#userForm').trigger("reset");
// $('#userCrudModal').html("Add New User");
// $('#ajax-crud-modal').modal('show');
// });
/* When click edit user */
$('body').on('click', '#edit-user', function () {
var dealerApp_id = $(this).data('id');
$.get('dealerApp/' + dealerApp_id +'/edit', function (data) {
$('#userCrudModal').html("Edit User");
$('#btn-save').val("edit-user");
$('#ajax-crud-modal').modal('show');
$('#user_id').val(data.id);
$('#fname').val(data.fname);
$('#email').val(data.email);
})
});
//delete user
$('body').on('click', '.delete-user', function ()
{
var dealerApp_id = $(this).data("id");
if(confirm("Are You sure want to delete !"))
{
$.ajax
({
type: "DELETE",
url: "{{ url('dealerApp/update')}}"+'/'+dealerApp_id,
success: function (data)
{
$("#dealerApp_id" + dealerApp_id).remove();
},
error: function (data)
{
console.log('Error:', data);
}
});
}
});
});
if ($("#userForm").length > 0) {
$("#userForm").validate({
submitHandler: function(form) {
var actionType = $('#btn-save').val();
$('#btn-save').html('Sending..');
$.ajax({
data: $('#userForm').serialize(),
url: "{{ url('dealerApp') }}",
type: "POST",
dataType: 'json',
success: function (data) {
console.log('Success:', data); // will remove this for production & add PNotify
// if (actionType == "create-user") {
// $('#users-crud').prepend(dealerApp);
// } else {
// $("#user_id_" + data.id).replaceWith(user);
// }
$('#userForm').trigger("reset");
$('#ajax-crud-modal').modal('hide');
$('#btn-save').html('Save Changes');
},
error: function (data) {
console.log('Error:', data); // will remove this for production
$('#btn-save').html('Save Changes');
}
});
}
})
}
Ultimately i will change the field to update the status of the user but for now i just need to get it functioning.
Thank you in advance.