Laravel - carbon addDays()
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Understanding Laravel's Carbon AddDays() in Practice
In this blog post, we will learn about using Laravel's Carbon addDays() to retrieve specific posts from a database based on their publication date within the past five days. By understanding how Carbon works and how it can be utilized effectively, you can create sophisticated applications with ease.
First, let's take a look at the example given in your code:
use Carbon\Carbon;
...
public function index(){
$date = Carbon::now();
$date->addDays(5);
$date->format("Y-m-d");
$posts = Post::where('status','=', 1)->whereDate('created_at','=', $date)->get();
return view('home', compact('date', $date))->with('posts', $posts);
}
...
In your code, you are trying to use Carbon's addDays() method on the variable $date. However, it seems that you're not seeing any output in the blade view. To understand why this is happening, let's break down the code:
1. You instantiate a Carbon instance with the current date using `$date = Carbon::now();`.
2. Next, you call addDays() method on $date by adding 5 to its value: `$date->addDays(5);`.
3. Finally, you format this new date in Y-m-d format and use it to filter posts within the past five days using whereDate() with your query builder.
Although your intentions are clear, a potential issue might be that you're calling the addDays() method on $date after setting its value and before it is actually used in the query. To fix this, follow these steps:
1. Move the call to addDays() right after defining $date: `$date = new Carbon(); $date->addDays(5);`. This will ensure that you're using the updated date object for filtering purposes.
2. Also, make sure you set up the correct view with the appropriate compact and with statements to render your data correctly on the blade template.
Here is a revised example code:
use Carbon\Carbon;
...
public function index(){
$date = new Carbon();
$date->addDays(5);
$posts = Post::where('status','=', 1)->whereDate('created_at','=', $date)->get();
return view('home', compact('date'))->with(['premiumPosts' => $posts]);
}
...
Now, your controller will retrieve all posts within the past five days for premium users while non-premium users are redirected. If you need to filter by status (which is currently set to 1 for published posts), consider revising this part of the code:
$posts = Post::where('status','=', 1)->whereDate('created_at','=', $date)->get();
return view('home', compact('date'))->with(['premiumPosts' => $posts]);
In this example, you have an array of posts named 'premiumPosts' rather than just posts. This is to help differentiate between premium and non-premium posts for your users on the front end.
Remember that Carbon allows developers to manipulate date and time objects in a flexible manner. With proper understanding and usage, you can create intelligent and powerful applications with Laravel and its Carbon library.