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

Ibe's avatar
Level 1

How to group tables under the same date

Hi there, does anyone know how to sort data per day from multiple tables?

So I have multiple tables (actone, acttwo, actthree,...). Each one of them contains the timestamps with the created_at. How can I group actone, acttwo, actthree,... under the same date? Can anyone help please?

How I would like my view: https://prnt.sc/j6f73w

ChildController:

public function show($id)
    {
      $child = Child::find($id);

      return view('admin.children.show')->withChild($child);
    }

Show.blade.php:

<?php foreach ($child->actone as $actone): ?>

          <td>{{$actone->category}}</td>
          <td>{{$actone->time}}</td>
          <td>{{$actone->name}}</td>
          <td>{{$actone->rating}}</td>
          <td>{{$actone->comment}}</td>
      <td>{{$actone->created_at}}</td>

        <?php endforeach; ?>


        <?php foreach ($child->acttwo as $acttwo): ?>

          <td>{{$acttwo->time_from}}</td>
          <td>{{$acttwo->time_to}}</td>
          <td>{{$acttwo->rating}}</td>
          <td>{{$acttwo->comment}}</td>
      <td>{{$acttwo->created_at}}</td>

        <?php endforeach; ?>

        <?php foreach ($child->actthree as $actthree): ?>

          <h1>{{$actthree->medication}}</h1>
          <h1>{{$actthree->time}}</h1>
          <h1>{{$actthree->prescription}}</h1>
          <h1>{{$actthree->comment}}</h1>
      <td>{{$actthree->created_at}}</td>

        <?php endforeach; ?>
    
    ...

Thanks!!

0 likes
2 replies
Vilfago's avatar

Not sure it will work, and probably not the easiest way :

$acts = collect([$child->actone, $child->acttwo, $child->actthree]);


$grouped = $acts->mapToGroups(function ($item, $key) {
                 return [$item['created_at'] =>$item['comment']];

https://laravel.com/docs/5.6/collections#method-maptogroups

I don't know for the moment how you will present your data with so many differents column in your tables

So probably not the best answer, but it could give you some clue.

Please or to participate in this conversation.