How to Get Last Week Data in Laravel?

By Hardik Savani April 16, 2024 Category : Laravel

I am going to explain you example of laravel get last week data. if you have question about laravel get last week records then i will give simple example with solution. This tutorial will give you simple example of get last week data in laravel. if you want to see example of how to get last week data in laravel then you are a right place. Follow bellow tutorial step of laravel 8 get last week records from database.

we can get last week records in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.

Here, i will give you very simple example of how to get last week records in laravel. we will use whereBetween() and Carbon in this example.

I have one created items table that structure as bellow you can see.

items table:

Controller Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Http;

use App\Models\Item;

use Carbon\Carbon;

class ITSController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$items = Item::select('*')

->whereBetween('created_at',

[Carbon::now()->subWeek()->startOfWeek(), Carbon::now()->subWeek()->endOfWeek()]

)

->get();

dd($items);

}

}

Output:

Array

(

[0] => Array

(

[id] => 10

[title] => Dr.

[body] => Body 3

[created_at] => 2021-10-08T09:37:55.000000Z

[updated_at] => 2021-08-12T13:46:08.000000Z

)

[1] => Array

(

[id] => 12

[title] => Dr.

[body] => Body 2

[created_at] => 2021-10-06T13:46:43.000000Z

[updated_at] => 2021-08-12T13:46:43.000000Z

)

[2] => Array

(

[id] => 13

[title] => Mrs.

[body] => Body 1

[created_at] => 2021-10-05T13:46:43.000000Z

[updated_at] => 2021-08-12T13:46:43.000000Z

)

)

I hope it can help you...

Tags :
Shares