php

Laravel Carbon subtract days from current date

Problem

I am trying to pull all the users whose created_at date has been more than 10 days from today. I know that I should be using Carbon package for this and here is my take:

Carbon::now() ==> I want as ==> Carbon::now() – 10days

$users = Users::where('status_id', 'active')
               ->where( 'created_at', '<', Carbon::now())
               ->get();

What’s the right way to get Laravel Carbon to work for me on this case ?

Solution

Laravel Carbon is an amazing library that you can use to play with dates and go back and forth on formatting and calculations. What you can use is subDays() method:

$users = Users::where('status_id', 'active')
           ->where( 'created_at', '>', Carbon::now()->subDays(10))
           ->get();

About the author

laravelrecipies