简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM