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

Ibe's avatar
Level 1

Show data per day with multiple tables?

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. I show already all the information for each child... but how do I show my content per day for that child? 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
12 replies
tykus's avatar

This is one possibility:

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

    // Define a Closure that will apply grouping to each Collection.
    $groupingFunction = function ($act) {
        return $act->created_at->toDateString();
    };

    // Apply grouping on each relation
    $groupedActone = $child->actone->groupBy($groupingFunction);
    $groupedActtwo = $child->acttwo->groupBy($groupingFunction);
    $groupedActthree = $child->actthree->groupBy($groupingFunction);

    return view('admin.children.show'(compact('child', 'groupedActone', 'groupedActtwo', 'groupedActthree'));
}

In the view you have each Act grouped by date, so you need nested loops

@foreach ($groupedActone as $date => $actones): ?>
    {{ $date }}
    @foreach($actones as $actone)
        <tr>
            <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>
        </tr>
    @endforeach
@endforeach
1 like
tykus's avatar

Silly me.. should be. toDateString() - updated above for accuracy

1 like
Ibe's avatar
Level 1

@tykus It kinda worked... but this is what im getting in my view: https://prnt.sc/j6hp23

So it sorts well on data, but if the date of actone is the same as acttwo, it doesn't show under the same label.. Do you know how to change it?

I tried to add the acttwo like this:

@foreach ($groupedActone as $date => $actones)
    <br>
    {{ $date }}
    <br>


    @foreach($actones as $actone)
    <strong>eten</strong>
        <tr>
            <td>{{$actone->category}}</td>
            <td>{{$actone->time}}</td>
            <td>{{$actone->name}}</td>
            <td>{{$actone->rating}}</td>
            <td>{{$actone->comment}}</td>
        </tr>
    @endforeach


@endforeach


@foreach ($groupedActtwo as $date => $acttwos)
  <br>
    {{ $date }}
  <br>

    @foreach($acttwos as $acttwo)
    <strong>slapen</strong>
        <tr>
            <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>
        </tr>
    @endforeach


@endforeach 
tykus's avatar

Ok, I misunderstood your requirement in that case; do you want all of the actone, acttwo and actthree grouped under the same date? Do they all have the same properties, or different?

1 like
Ibe's avatar
Level 1

@tykus Im sorry.. so yeah I want all the actone, acttwo and actthree under the same date. They have different properties

Ibe's avatar
Level 1

@tykus Could you please take another look at this please? Your solutions seem to work very wel.. thanks

tykus's avatar

@Ibe if you merge the three collections before grouping, then you would have one collection to pass to the view:

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

    // Define a Closure that will apply grouping to each Collection.
    $groupingFunction = ;

    $allActs = $child->actone->toBase()
    ->merge($child->acttwo->toBase())
    ->merge($child->$actthree->toBase())
    ->groupBy(function ($act) {
            return $act->created_at->toDateString();
    });

    return view('admin.children.show'(compact('child', 'allActs'));
}

Then inside the view, you would need to check the Model before deciding what to output - one possibility would be to have a partial view created for each resources/views/acts/partials/ActOne.blade.php, resources/views/acts/partials/ActTwo.blade.php and resources/views/acts/partials/ActThree.blade.php, and include the partial dynamically depending on the current model in the loop:

@foreach ($allActs as $date => $acts)
    {{ $date }}
    @foreach($acts as $act)
    @php
        $viewName = (new ReflectionClass($act))->getShortName();
    @endphp

    @include("acts.partials.{$viewName}")
    @endforeach
@endforeach
1 like
Ibe's avatar
Level 1

@tykus Hi there, it kinda worked... So I tried it this way with partials, but the strange thing is that ALL acts get displayed under the date (and not only the ones that belongs to that date...)? I can't figure out why... http://prntscr.com/j6zc6c

So what I have:

The controller

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


      // Define a Closure that will apply grouping to each Collection.

   $allActs = $child->actone->toBase()
   ->merge($child->acttwo->toBase())
   ->merge($child->actthree->toBase())
   ->groupBy(function ($act) {
           return $act->created_at->toDateString();
   });

      $next = Child::where('id', '<', $child->id)->orderBy('id', 'desc')->first();
      $previous = Child::where('id', '>', $child->id)->first();
      return view('admin.children.show', compact('child', 'allActs'))->with('next', $next)->with('previous', $previous);
    }

show.blade.php:

@foreach ($allActs as $date => $acts)
<strong>{{ $date }}</strong>

    @foreach($acts as $act)
    @php
        $viewName = (new ReflectionClass($act))->getShortName();
    @endphp

    @include("admin.activities.partials.{$viewName}")
    @endforeach
@endforeach 

partials/actone.blade.php: (for each act)

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

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

<?php endforeach; ?>
tykus's avatar

What does the Collection look like after the merge and grouping?

1 like
Ibe's avatar
Level 1

@tykus How can I check this? (sorry im new..) But its difficult to explain what im getting in my view because I am confused of what data im getting back... But i'll give it a try: So I think that my data is looping 2x somewhere... -> If I start 'fresh' and add a new activity (lets say actone), it gets displayed under the correct date. But then when I add another actone, this gets displayed under the correct date, but then each acts gets displayed 2 times... Then this is what im getting in my view: http://prntscr.com/j73dca

Ibe's avatar
Level 1

@tykus Do you maybe have another solution? Im still struggling with this.. sorry :(

Please or to participate in this conversation.