Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 437
archvist Avatar asked Nov 24 '25 17:11

archvist


1 Answers

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

like image 155
Vit Avatar answered Nov 27 '25 06:11

Vit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!