Home > Mobile >  Laravel: get users created more than three months ago
Laravel: get users created more than three months ago

Time:07-31

in my laravel app I want to get all records of users where "created_at" timestamp is older than three months from current date, is it possible to achieve this with Eloquent query builder?

CodePudding user response:

First, you can use Carbon to get the date of 3 months ago.

$date = Carbon::now()->subMonths(3)->format('Y-m-d'); 

And then use the date to the query

$users = User::where('created_at', '<=', $date)->get();

CodePudding user response:

belowe code can help you to solve problem

$three_month = Carbon::now()->startOfMonth()->subMonth(3);
 $data = DB::table('orders')
                    ->where('placed_at','>',$three_month)
                    ->get();

CodePudding user response:

$dateS = Carbon::now()->startOfMonth()->subMonth(3);
$dateE = Carbon::now()->startOfMonth(); 
$usersArr = $users
->whereBetween('created_at',[$dateS,$dateE])
->get();

startOfMonth() begins with 1st date of the month

CodePudding user response:

To find users older than three months

$users = User::where('created_at','<', today()->subMonths(3))->get();

CodePudding user response:

The easiest way to make it done by using carbon. Use the below code-

User::where( 'created_at', '<=', Carbon::now()->subMonths(3))->get();

CodePudding user response:

$threeMonthsAgo = now()->subMonth(3);    
$users = User::where('created_at', '<' , $threeMonthsAgo)->get();
  • Related