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 get only records created today?

Hi there, I don't really know how to show the records that are only created today... Can someone help please?

So I have a list of children and each child has a checkin and checkout, what I would like is to show only the checkins and checkouts that are created today.. they both have a created_at

CheckController:

public function index()
    {
      $children = Child::all();
      $checkins = Checkin::all(); //only show for today
      $checkouts = Checkout::all(); //only show for today
      return view('admin.checks.index')->withCheckins($checkins)->withCheckouts($checkouts)->withChildren($children);
    }

Thanks!

0 likes
15 replies
rin4ik's avatar

I'm not sure but u can try this

      $checkins = Checkin::where('created_at', Carbon::today())->get();
      $checkouts = Checkout::where('created_at', Carbon::today())->get();
1 like
Cronix's avatar
Cronix
Best Answer
Level 67

@rin4ik that won't work because created_at contains the time, not just the date.

use whereDate(), which casts it to an actual date without the time.

$checkins = Checkin::whereDate('created_at', Carbon::today())->get();
// select * from `checkins` where date(`created_at`) = '2018-05-01'

https://laravel.com/docs/5.6/queries#where-clauses

8 likes
Cronix's avatar

Strange, I actually tried it on my user table and it returned the correct results.

Try first checking what date you get when using {{ Carbon::today() }} Then check and make sure you actually have created_at dates in the db that match that date.

I didn't have any made "today", so I manually used a date.

App\User::whereDate('created_at', '2017-07-14')->get()

returned all users who were created on that date.

2 likes
Cronix's avatar

Also this

return view('admin.checks.index')->withCheckins($checkins)->withCheckouts($checkouts)->withChildren($children);

could be written a lot simpler

return view('admin.checks.index', compact('checkins', 'checkouts', 'children'));
1 like
Cronix's avatar

The collection is getting the right one --> http://prntscr.com/jcghq0 But in my blade its just show them all... do I need to change something there?

Hard to know without seeing what you're doing in the view, along with your updated queries.

try php artisan view:clear to clear the cached views.

1 like
Ibe's avatar
Level 1

@Cronix Sorry so this is what I have now:

(view clear didn't do anything)

CheckController:

public function index()
    {
      $children = Child::all();
      $checkins = Checkin::whereDate('created_at', Carbon::today())->get();
      $checkouts = Checkout::whereDate('created_at', Carbon::today())->get();
      return view('admin.checks.index', compact('checkins', 'checkouts', 'children'));
    }

blade:

@foreach ($children as $child)
  <div class="field card">
      <figure class="image is-64x64">
        <img src="/images/children/{{$child->photo}}" class="rounded-image" alt="Placeholder image">
      </figure>
      {{$child->name}}


  <h1>Checkin at::</h1>
  {{$child->checkin->count() == 0 ? 'Not checked in!' : ''}}
  <?php foreach ($child->checkin as $checkin): ?>

      <h1>{{$checkin->checkin}}</h1>

      <a class="button is-outlined is-rounded m-r-5" href="{{route('checkin.show', $checkin->id)}}">View</a>
      <a class="button is-light" href="{{route('checkin.edit', $checkin->id)}}">Edit</a>
  <?php endforeach; ?>


  <h1>Checkout at:</h1>
  {{$child->checkout->count() == 0 ? 'Not checked out!' : ''}}
  <?php foreach ($child->checkout as $checkout): ?>

      <h1>{{$checkout->checkout}}</h1>
      <a class="button is-outlined is-rounded m-r-5" href="{{route('checkout.show', $checkout->id)}}">View</a>
      <a class="button is-light" href="{{route('checkout.edit', $checkout->id)}}">Edit</a>
  <?php endforeach; ?>

  </div>

  @endforeach 
Cronix's avatar

You're iterating over $children, and $children is $children = Child::all(); so yes, it would loop over all of them that you have.

This is a different problem than searching by the date, which seems to be working now according to your dump of checkin.

1 like
Ibe's avatar
Level 1

@Cronix yh I see the problem, but have no idea how to fix it.. Do you know how to approach this?

Cronix's avatar

It would be better in a new thread since your original question is solved, and others might find this post who have the same problem, but you should be using relationships to get the users who have checkins and/or checkouts for today.

1 like

Please or to participate in this conversation.