How to Get Current Week Records in Laravel?

By Hardik Savani October 7, 2021 Category : Laravel

Hi,

Now, let's see example of how to get current week data in laravel. i explained simply about laravel get current week data. This post will give you simple example of get current week data in laravel. you can see laravel wherebetween current week data. Let's get started with laravel where between dates.

we can get current week records in laravel 6, laravel 7 and laravel 8 application.

Here, i will give you very simple example of how to get current week record in laravel. we will use whereBetween() and Carbon Object for date 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()->startOfWeek(), Carbon::now()->endOfWeek()]

)

->get();

dd($items);

}

}

Output:

Array

(

[0] => Array

(

[id] => 9

[title] => Dr.

[body] => Body 4

[created_at] => 2021-10-06T18:13:40.000000Z

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

)

[1] => Array

(

[id] => 10

[title] => Dr.

[body] => Body 3

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

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

)

[2] => Array

(

[id] => 12

[title] => Dr.

[body] => Body 2

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

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

)

[3] => 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 :