In my database i have table called signal with this structure
Id | date | status | name |
---|---|---|---|
1 | 2/12/2023 | 1 | ben |
2 | 2/12/2023 | 1 | john |
3 | 3/12/2023 | 1 | peter |
4 | 3/12/2023 | 1 | love |
5 | 3/12/2023 | 1 | sad |
6 | 4/12/2023 | 1 | water |
7 | 4/12/2023 | 1 | tom |
8 | 5/12/2023 | 1 | jerry |
9 | 5/12/2023 | 1 | hen |
10 | 5/12/2023 | 1 | bete |
From my controller i have gotten the needed data
$history = Signal::where('status', 1)->paginate(getPaginate(7));
I can show the value on a table normally but what i want is
for each date, create a table with that date as the header
just like the tables below
**2/12/2023**
id | name |
---|---|
1 | ben |
2 | john |
**3/12/2023**
id | name |
---|---|
3 | peter |
4 | love |
5 | sad |
**4/12/2023**
id | name |
---|---|
6 | water |
7 | tom |
I have tried to make a normal table but i don't know how to do this with dynamic headers
You can use Laravel's groupBy() method, like this:
$results = Signal::where('status', 1)
->paginate(getPaginate(7))
->groupBy('date');
This will return all the results grouped by the same date, in a format:
results = [
date1 => [
item1,
item2,
//etc
],
date2 => [
item3,
item4,
//etc
]
]
Then in blade, you can loop through these results like this:
@foreach ($results as $date => $items)
<p>Date: {{$date}}</p>
<table>
<thead>
<tr>
<th>ID</th>
<td>name</td>
</tr>
</thead>
<tbody>
@foreach ($items as $item)
<tr>{{$item->id}}</tr>
<tr>{{$item->name}}</tr>
@endforeach
</tbody>
</table>
@endforeach
Note: Table layout not tested, but you get the general idea.