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

geekshubh's avatar

Foreach loop Table Display

Hi guys this is my database structure:-

dayofweek       subject_id      start_time      end_time
1                   1           (some_time)     (some_time)
1                   2           (some_time)     (some_time)
1                   3           (some_time)     (some_time)
1                   4           (some_time)     (some_time)
2                   1           (some_time)     (some_time)

and so on till dayofweek is 7

Here is my index.blade.php

<div class="panel-body">
            <table class="table table-bordered table-striped {{ count($timetable) > 0 ? 'datatable' : '' }} dt-select">
                <tbody>
                      <tr>
                          <th style="width: 10px">Day</th>
                          <th>Timetable</th>
                      </tr>
                      <tr>
            // There will be a loop here for showing dayOfWeek(it will show from Monday to Sunday)
                          <td>{{$timetable->dayOfWeek}}</td>
                        // Here I want to loop only the subjects which exist for that particular dayOfWeek as a Button  
            <td>
                            <div class="btn-group" style="margin:5px;">
                                <button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{$timetable->subject_id}} - {{$timetable->start_time}} -> {{$timetable->end_time}}</button>
                                    <div class="dropdown-menu">
                                          <a class="dropdown-item">Edit</a>
                                          <a class="dropdown-item">Delete</a>
                                    </div>
                            </div>
                          </td>
                      </tr>
                </tbody>
            </table>
        </div>

Here is the image of how I want it to look like(ignore the add button) https://imgur.com/a/LIOANng

0 likes
4 replies
ryanborum's avatar

So what is the issue?

In addition, if you are using JQuery DataTables, they're very strict with table formatting and you'll need to define a thead element as well as your tbody to have it work properly.

1 like
geekshubh's avatar

@ryanborum The issue is how do I frame the foreach loop to get the subject_id for that particular dayOfWeek, I will fix the Jquery Datatables thing!

ryanborum's avatar

If you have a model for the entries in your database, you can pull only the models for a given dayOfWeek and then iterate through them/ print their times, subject, etc.

If you want to do it in your view (probably better in controller, but eh)

@php $sundayClasses = \App\NameOfModel::all()-where('dayOfWeek', '1'); @endphp

You can then go through each Model that has dayOfWeek = 1 with:

@foreach ($sundayClasses as $sundayClass)
    <td>{{ $sundayClass->subject }}</td>
@endforeach 
1 like

Please or to participate in this conversation.