Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lewanay's avatar

Laravel editing user in the modal

Hello everyone , I am getting users through ajax. Now i am trying to edit the user in a modal. If someone click on the edit button then a modal will be open. I already done that and also getting the user in the modal but it give me only the first row added after i click on different tho. A/nd edit only that first one. Below is my code. Its javascript where my table tbody is available and you can see the edit button and reference to the modal.

//Javascript

$(document).ready(function () {

            //FOR LOADING STUDENTS
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

        $('select[name="class_id"]').on('change', function() {
            var classID = $(this).val();
            if (classID) {

                $.ajax({

                    url: '/attendance/ajax/' + classID,
                    type: "GET",
                    dataType: "json",
                    success: function (data) {

                        var markup = '';
                        markup = '<tr class="filters"><th style="width: 2%" class="align-middle text-center"><input type="checkbox" id="options"></th><th style="width: 15%" class="text-center">Student ID<input type="text" class="form-control" disabled></th> <th style="width: 15%" class="text-center">Student Name<input type="text" class="form-control" disabled></th> <th style="width: 15%" class="text-center">Attendance<input type="text" class="form-control" disabled></th> <th style="width: 15%" class="text-center">Date<input type="text" class="form-control" disabled></th> <th style="width: 15%;" class="align-middle text-center">Actions</th> <tr>';

                        $.each(data, function (key, value) {

                            markup += '<tr> <td><input class="checkBoxes" type="checkbox" name="checkBoxArray[]" value="' + value.id + '"></td> <td><input type="hidden" value="' + value.student_id + '" name="student_id[]">' + value.student_id + '</td> <td><input type="hidden" value="' + value.first_name + '" name="first_name[]"><input type="hidden" value="' + value.last_name + '" name="last_name[]">' + value.first_name + ' ' + value.last_name + '<td><input type="hidden" value="' + value.attendance + '" name="attendance[]">' + value.attendance + '</td>' + '<td><input type="hidden" value="' + value.created_at + '" name="created_at[]">' + value.created_at + '</td>' + '<td style=" width=12%" class="text-center"> <a data-toggle="modal" data-target="#editAttendanceModal"' + value.id + '""><button title="Edit" class="btn btn-outline-primary"><span class="fas fa-pencil-alt"></span></button></a> </td>' + '</td> <tr>';

                        });
                        $('table[id="studentsData"]').html(markup);
                    }
                });
            }

        });

//Modal

@foreach($attendances as $attendance) <div class="modal fade" id="editAttendanceModal"{{$attendance->id}}>

            <div class="modal-header header-backgroud text-white">
                <h5 class="modal-title">Class View</h5>
                <button class="close" data-dismiss="modal">
                    <span>&times;</span>
                </button>
            </div>
            <div class="container">

            {!! Form::model($attendance, ['method'=>'PATCH', 'action'=>['AttendanceController@update',  $attendance->id]]) !!}
            <!--CLASS DETAIL-->
                <div class="row mt-2">
                    <div class="col-md-12">
                        <h3 class="display-4 text-center">Edit Attendance</h3>
                        <div class="form-group">
                            <label for="attendance">Attendance</label>
                            {{Form::select('attendance',['present' => 'Present', 'absent' => 'Absent', 'leave'=>'Leave'], null,['class' => 'form-control'])}}
                        </div>
                        {!! Form::button(' Save Changes', ['type'=>'submit', 'class'=>'btn btn-outline-danger btn-block mb-3']) !!}
                    </div>
                </div>
                {!! Form::close() !!}
            </div>

        </div>
    </div>
</div>

@endforeach

//Controller

public function update(Request $request, $id) { $attendance = StudentsAttendance::findOrFail($id); $attendance->update($request->all()); return redirect()->back(); }

0 likes
3 replies
ftiersch's avatar
ftiersch
Best Answer
Level 28

You have put the ID of the user outside of your id="" attribute.

id="editAttendanceModal"1

So for the browser there are only multiple "editAttendanceModal" modals because it doesn't understand that the 1 should belong to it.

lewanay's avatar

Such a small thing I did not notice. Even I did this modal for many views also. Thank you Sir. :)

ftiersch's avatar

Haha, it's always the small things that get you :)

1 like

Please or to participate in this conversation.