How to show data for the past 2 months only in laravel API?

Viewed 31

i am getting data from API to show in blade

controller:

public function index()
{
    $response = Http::post('http://example.com/authenticate', [
        'Username' => 'ADMIN',
        'Password' => 'ADMIN',
        'Token' => 'FK98DL..',
    ]);
    $token = json_decode($response, true);
    $apiURL = 'http://example.com/api/SalesOrder/';
    $headers = [
        'Content-Type' => 'application/json',
        'Authorization' => $token,
    ];
    $response2 = Http::withHeaders($headers)->get($apiURL);
    $data = $response2->json();
    return view('api.auth.orders.index', compact('data'));
}

blade view:

@foreach ($data as $item)
<tr>
<td>{{$item['DocDate']}}</td>
</tr>                                             
@endforeach

Output:

enter image description here

now my question is, how can i show the above data for the past 2 months of the current year (August and July of 2022)?

i tried to use Carbon in my previous question: how to display data in table for the past 3 months only in laravel?

but it ignore the year, is there any other approach for this problem?

1 Answers

you either change the code in API to return only last 2 months or add some change in your blade to check the months

@foreach ($data as $item)
@if(date('m') == date('m', strtotime($item['DocDate'])) || date('m')-1 == date('m', strtotime($item['DocDate'])))
<tr>
<td>{{$item['DocDate']}}</td>
</tr>
@endif                                             
@endforeach
Related