how to display data in table for the past 3 months only in laravel?

Viewed 49

i am working on external API and i am getting the data that i need as showing below

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();
    $current_timestamp = Carbon::now()->format('j/n/Y');
    return view('api.auth.orders.index', compact('data','current_timestamp'));
}

and in view i am trying to compare between current date and one of the arrays from the API to make sure that the logic is correct as showing below:

<h1>{{$current_timestamp}}</h1>
<h1>{{Carbon\Carbon::createFromFormat('d/m/Y h:i:s a', $data[29]['DocDate'] )->format('j/n/Y')}}</h1>
@if ($current_timestamp > Carbon\Carbon::createFromFormat('d/m/Y h:i:s a', $data[29]['DocDate'] )->format('j/n/Y'))
<h1>Hello!</h1>
@endif

output of the above:

19/8/2022
18/8/2022
Hello!

so far everything looks good and working

but now i am a bit confused how can i used the above with for each to display the data in a table and show only the past three months (June, July and August)

any suggestion?

UPDATE (After @T.Shah answer):

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();

    $past_three_months = Carbon::now()->subMonths(3)->format('j/n/Y');
    return view('api.auth.orders.index', compact('data','past_three_months'));

}

and blade:

@foreach ($data as $item)
@if (Carbon\Carbon::createFromFormat('d/m/Y h:i:s a', $item['DocDate'])->format('j/n/Y') <=
$past_three_months)
<tr>
<td>{{ Carbon\Carbon::createFromFormat('d/m/Y h:i:s a', $item['DocDate'])->format('j/n/Y') }}
 </td>
 </tr>
 @endif
 @endforeach

output:

enter image description here

why am i still getting data from 2020? is there anything wrong with the logic?

UPDATE 2:

enter image description here

1 Answers

You can pass another date timestamp from controller - past 3 months date from current date.

$current_timestamp = Carbon::now()->format('j/n/Y');
$past_three_months = carbon::now()->subMonths(3)->format('j/n/Y');

Then in blade view you can compare the api response date with past 3 months date and show results.

  @if ( Carbon\Carbon::createFromFormat('d/m/Y h:i:s a', $data[29]['DocDate'] )->format('j/n/Y') >= $past_three_months)
  <h1>Hello!</h1>
  @endif
Related