laravel get data from last 7 days – How to Get Last 7 Days Record in Laravel?

laravel get data from last 7 days using Carbon::now()->subDays(7) and ->whereRaw(‘DATE(join_date) = DATE_SUB(CURDATE(), INTERVAL 7 DAY)’) Examples.

laravel get data from last 7 days

i can easily get last 7 days records in laravel 6, laravel 7 and laravel 8 version. it should be last N days records.

Controller File:

Example

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Carbon\Carbon;
  
class MemberController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $date = Carbon::now()->subDays(7);
  
        $Members = Member::where('join_at', '>=', $date)->get();
    
        dd($Members);
    }
}

You can use whereDate for that :

->whereDate('join_at', Carbon::now()->subDays(7))
->get();

Example 2

 ->whereRaw('DATE(join_date) = DATE_SUB(CURDATE(), INTERVAL 7 DAY)')

Don’t Miss : Laravel Get Last 7 Days Month Year Record From MySQL?

Free Live Chat for Any Issue

I hope you get an idea about laravel get data from last 7 days.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donโ€™t forget to share.