Laravel: carbon addDays()

I am trying to display posts for only 5 days after they are created. I have added $date->addDays(5) to my controller, but nothing is showing in my blade. Here is my controller:

use Carbon\Carbon;
public function index(){

   $date = Carbon::now();
   $posts = Post::where('status','=', 1)->whereDate('created_at','=', $date)->get();
   return view('home', compact('date', $date))->with('posts', $posts);

And this is my home.blade.php:


<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">All posts
                    <a href="/posts/create" class="btn btn-primary" style="margin-left: 70%">New post +</a></div>
                <div class="card-body">
                    @foreach($posts as $post)
                        @if($post->status == 1)
                            <small style="color:red">PREMIUM POST</small>
                        <hr style="border: 1.5px solid grey">
                    <small><?php echo now() ?></small>

Why are my posts not being displayed in my blade? I am expecting them to show for 5 days after creation.

The whereDate() method expects a date in the format Y-m-d, but $date is not being formatted correctly. To fix the issue, change this line:



$dateFormatted = $date->format("Y-m-d");

Then, use $dateFormatted in your whereDate() method:

$posts = Post::where('status','=', 1)->whereDate('created_at','=', $dateFormatted)->get();

This should display the posts created in the last 5 days.