I was trying to fit an existing PHP project into my Laravel project. I’ve tried so that I can gather knowledge how to fit a project into the Framework. Now, whenever I wanted to update or delete specific student by their respective id, it always update or delete the latest entry. Why does it happen and how can I solve this?
This is my routes/web.php
Route::post('admin/update-student/{id}',[StudentListController::class,'update'])->name('update-student');
This is my StudentListController@update
public function update(Request $request, int $id)
{
$student = StudentList::find($id);
$student->student_name = $request->student_name;
$student->course_section = $request->course_section;
$student->save();
return redirect()->route('student-list')->with('success', 'Student updated successfully!');
}
This is my views
<div class="modal fade" id="updateStudentModal" data-backdrop="static" data-keyboard="false" tabindex="-1"
aria-labelledby="updateStudent" aria-hidden="true">
<div class="modal-body">
<form action="{{ route('update-student', $student->id) }}" method="POST">
@csrf
<input type="hidden" class="form-control" id="updateStudentId" name="tbl_student_id">
<div class="form-group">
<label for="updateStudentName">Full Name:</label>
<input type="text" class="form-control" id="updateStudentName" name="student_name" value="{{ $student->student_name }}">
</div>
<div class="form-group">
<label for="updateStudentCourse">Course and Section:</label>
<input type="text" class="form-control" id="updateStudentCourse" name="course_section" value="{{ $student->course_section}}">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-dark">Update</button>
</div>
</form>
</div>
</div>
Here is some JS for Update modal for update button
function updateStudent(id) {
$("#updateStudentModal").modal("show");
let updateStudentId = $("#studentID-" + id).text();
let updateStudentName = $("#studentName-" + id).text();
let updateStudentCourse = $("#studentCourse-" + id).text();
$("#updateStudentId").val(updateStudentId);
$("#updateStudentName").val(updateStudentName);
$("#updateStudentCourse").val(updateStudentCourse);
}
dowithSunny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.