archvist
archvist

Reputation: 722

Laravel fetch data where carbon last month

I'm trying to fetch the first result from my data where the date is equal to last months date range.

This is can example of my database contents -

id  date_recorded
1   2016-07-22 15:21:33
2   2016-08-13 12:22:22
3   2016-07-06 12:22:22
4   2016-09-12 12:45:22

This is my query to fetch the 2nd result (the most recent from the last month).

$seoScoreLastMonth = Manual::where('account_id', Auth::user()->account)
->where('date_recorded', '>=', Carbon::now()->subMonth())
->orderBy('date_recorded', 'desc')
->pluck('seo_score')
->first();

The result returned is null, this is because the first result in the database by id does not match the date?

Upvotes: 3

Views: 3916

Answers (1)

Vit
Vit

Reputation: 488

I needed this month. Here is my query. I think You'll be able to modify it if you need

$start = new Carbon('first day of this month');
$start = $start->startOfMonth()->format('Y-m-d H:i:s'); // startOfMonth for 00:00 time

Your query..->where('date_recorded','>',$start)..

With help of Carbon - get first day of month enter link description here

Upvotes: 1

Related Questions