I do not know what I am missing, but I cant make this work. It's throwing an error, POST 500 (Internal Server Error) "Too few arguments to function" in Laravel.
$(document).on('click', '.btnUpdateStudent', function (e) {
e.preventDefault();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$td=$(this).closest('td');
$tr=$(this).closest('tr');
var id=$td.attr("data-studId");
var inputEditName="<input type='text' class='inputEditName form-control'>";
$tr.find('td:eq(1)').val(inputEditName);
$tr.find('td:eq(1)').find("input.inputEditName").val();
var inputEditEmail="<input type='text' class='inputEditEmail form-control'>";
$tr.find('td:eq(2)').val(inputEditEmail);
$tr.find('td:eq(2)').find("input.inputEditEmail").val();
var options = $('#selectDepartment option');
var optionValues = $.map(options ,function(option){
return "<option value='" option.value "'>" option.text "</option>";});
var selectDepartment ="<select class='selectDepartment form-select form-control'></select>";
var updateDept=$('.selectDepartment').val();
$tr.find('td:eq(3)').val(selectDepartment);
$tr.find('td:eq(3)').find("select.selectDepartment").html(optionValues);
$tr.find('td:eq(3)').find("selectDepartment").val();
$tr.find('td:eq(3)').find("select.selectDepartment").val(updateDept);
var editname=$tr.find('td:eq(1)').find("input.inputEditName").val();
var editemail=$tr.find('td:eq(2)').find("input.inputEditEmail").val();
var editdepartment=$tr.find('td:eq(3)').find("select.selectDepartment").val();
var data = {
'id' : id,
'name' : editname,
'email': editemail,
'department_id': editdepartment
}
console.log(data);
$.ajax({
type: "POST",
url: "/update-student",
data: data,
dataType: "json",
success: function (response) {
console.log(response);
if (response.status == 400) {
$('#saveform_errList').html("");
$('#saveform_errList').addClass('alert alert-danger');
$.each(response.errors, function (key, err_values){
$('#saveform_errList').append('<li>' err_values '</li>');
});
}else {
$('#saveform_errList').html("");
$('#success_message').addClass('alert alert-success')
$('#success_message').text(response.message)
$("#inputForm").find('input').val("");
}
}
});
});
this is my controller. did i missed something? or is there part that should be there?
public function update(Request $request)
{
$validator = Validator::make($request->all(), [
'name'=> 'required|max:100',
'email'=>'required|email|max:100',
'department_id'=>'required'
]);
if($validator->fails())
{
return response()->json([
'status'=>400,
'errors'=>$validator->messages()
]);
}
else
{
$student = Student::find();
if($student)
{
$student->id = $request->input('id');
$student->name = $request->input('name');
$student->email = $request->input('email');
$student->department_id = $request->input('department_id');
$student->update();
return response()->json([
'status'=>200,
'message'=>'Student Updated Successfully.'
]);
}
else
{
return response()->json([
'status'=>404,
'message'=> 'No Student Found.'
]);
}
}
}
I tried reviewing my code but I really can't distinguish the problem. I need help
CodePudding user response:
$student = Student::find()
is where your issue is. The find() method requires one parameter which is the primary key id
gotten from your database.
Now, since you already passed the id
via your ajax just change your code to this:
public function update(Request $request)
{
$validator = Validator::make($request->all(), [
'name'=> 'required|max:100',
'email'=>'required|email|max:100',
'department_id'=>'required'
]);
if($validator->fails())
{
return response()->json([
'status'=>400,
'errors'=>$validator->messages()
]);
}
else
{
$student = Student::find($request->id); // here I have added the required parameter
if($student)
{
$student->id = $request->input('id');
$student->name = $request->input('name');
$student->email = $request->input('email');
$student->department_id = $request->input('department_id');
$student->update();
return response()->json([
'status'=>200,
'message'=>'Student Updated Successfully.'
]);
}
else
{
return response()->json([
'status'=>404,
'message'=> 'No Student Found.'
]);
}
}
}
UPDATE:
To refresh the page automatically after the Ajax
request is successful, do this:
Add this directly above where you have if (response.status == 400) {
:
if (response.status == 200) {
location.reload();
}
This will reload the page automatically.