How to Get Last Week Data in Laravel eloquent?

I am going to explain you example of laravel eloquent get last week data.

if you have question about laravel get last week records then i will give simple example with this laratuto is the give of the day solution of the day. laratuto provide all solution for you. 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.

we can get last week records in laravel 6, laravel 7 and laravel 8 application.all application run into the code…

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

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

This content database…..

Controller LaraTutoController.php

<?php
  
namespace App\Http\Controllers\Api;
  
use Illuminate\Support\Facades\Http;
use App\Models\Item;
use Carbon\Carbon;
    
class LaraTutoController extends Controller
{
    /**
     * Write code in Method
     *
     * @return response()
     */
    public function index()
    {
        $items = Bookable::select('*')
                        ->whereBetween('created_at', 
                            [Carbon::now()->subWeek()->startOfWeek(), Carbon::now()->subWeek()->endOfWeek()]
                        )
                        ->get();
  
        print_r($items);
    }
}

Output:

Array

(

    [0] => Array

        (

            [id] => 1
            [title] => "East Lessiemouth Fancy Rooms"
            [discription] => "Cum amet sed eveniet magnam. "
            [created_at] => 2021-06-07 17:06:38
            [updated_at] => 2021-06-07 17:06:38

        )

    [1] => Array

        (

            [id] => 2
            [title] => "Ilenemouth Rio Colina"
            [discription] => "Numquam aut consequatur quidem minima at tempora."
            [created_at] => 2021-06-07 17:06:38
            [updated_at] => 2021-06-07 17:06:38

        )

    [2] => Array

        (

            [id] => 3
            [title] => "East Eddie Fancy Rooms"
            [discription] => "Rerum praesentium dolore est distinctio accusamus excepturi."
            [created_at] => 2021-06-07 17:06:38
            [updated_at] => 2021-06-07 17:06:38

        )

)

Leave a Comment