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

lewanay's avatar

View problem

Hello everyone . Here is my code. Here i can get data into the but my problem is when i select class in my table then it just ruined my page layout and it become so weird...Indeed i am getting data but just can't maintain my page layout. Pls take a review

//My controller public function index(Request $request) { $class_id = $request->class_id; $students = DB::table('students')->join('students_classes', 'students_classes.id', 'students.students_class_id') ->where('students.students_class_id', $class_id)->get(); return view('admin.students.attendance.index', compact('students')); }

//Routes

Route::resource('/students/attendance', 'AttendanceController', ['names'=>[

'index'=>'admin.students.attendance.index',

]]);

//My view and ajax

<div class="ml-4 mr-4 py-2">
    <h3 class="text-center display-4">Take Attendance</h3>

    <select name="classes" id="classes">
    @foreach(App\StudentsClass::all() as $class)

            <option id="class{{$class->id}}" value="{{$class->id}}">{{$class->class_name}}</option>

    @endforeach
    </select>

    <table id="studentsData" class="table table-striped table-bordered table-list-search">
        <thead>
            <tr>
                <th>#</th>
                <th>Student ID</th>
                <th>Student Name</th>
                <th>Attendance</th>
            </tr>
        </thead>
            @foreach($students as $student)
        <tbody>
            <tr>
                <th>{{$student->id}}</th>
                <td>{{$student->student_id}}</td>
                <td>{{$student->first_name}} {{$student->last_name}}</td>
                <td>
                    <div class="form-group">
                        <select class="form-control" id="gender">
                            <option>Present</option>
                            <option>Absent</option>
                            <option>Leave</option>
                        </select>
                    </div>
                </td>
            </tr>
        </tbody>
            @endforeach
    </table>
    <a class="fas fa-folder-open btn btn-success float-right mb-4 mr-2"> Save</a>
</div>

@stop

@section('script')

<script>

    $(document).ready(function () {
        @foreach(App\StudentsClass::all() as $class)
        $("#class{{$class->id}}").click(function () {
            var classes = $("#class{{$class->id}}").val();
            $.ajax({

                                url: '{{url('/students/attendance')}}',

                                type: "GET",

                                dataType: "html",
                data: 'class_id=' + classes,

                success:function(response) {

                                    // console.log(response);
                    $('#studentsData').html(response);

                }

                 });
        });
        @endforeach
    });
0 likes
6 replies
jlrdw's avatar

Have you tried to build the table using php (laravel), the whole table. Then display the whole table in a Div.

$("#your_division").html(your_table);

In other words whole table is passed back to ajax call as one variable.

Snapey's avatar

Please format your code by putting 3 backticks ``` on a line before and after each code block

Snapey's avatar

You are doing a PHP blade loop, querying a model and then outputting a block of javascript for each student in your database.

I've not seen so many rules broken in one bit of code in a long time.

Look at the html source in your browser and you might see the problem.

What is it that you are trying to do that cannot just all be created server side?

jlrdw's avatar

@SNAPEY - @BOBBYBOUWMANN has answered in a duplicate question. OP won't mark his duplicates as answered.

https://laracasts.com/discuss/channels/laravel/laravel-view-problem

@lewanay please next time be a little patient you ask the same thing three times in 15 minutes I believe it was.

There are some at work, some still asleep this is an all-volunteer help, so please give folks the courtesy and some time to answer.

Please show your duplicates as answered with the reference to the answered question.

Another Forum member took quality time to answer your question which was already answered.

lewanay's avatar

@JLRDW - Yeah but now i used another approach thats why i have view problem now

lewanay's avatar

@SNAPEY - Sir I am learning laravel now . And i javascript too . Sorry if you have seen something wrong or rules broken in my code. Thats why i join this community to learn from you well experienced seniors :)

Please or to participate in this conversation.