Compare data in Blade/View in Laravel

Viewed 172

I'm grouping by date in the view. I want to compare it according to the incoming data and give the background color green if the data is larger than the data in the next row, red if it is smaller, and orange if it is equal. However, in the examples I examined, I could not compare the data coming with data_get() as I gave the table example below. Thanks a lot for your help.

<table>
    <thead>
        <tr>
            <th>Supplier</th>
            @foreach($dates as $date)
               <th>{{ $date->format('d.m.Y') }}</th>
            @endforeach
        </tr>
    </thead>
    <tbody>
    @foreach($suppliers as $supplier)
        <tr>
            <td>{{ $supplier->name }}</td>
            @foreach($dates as $date)
                <td>
          {{ data_get($transactionsByDate, $date->toDateString()) }}</td>
            @endforeach
        </tr>
    @endforeach
    </tbody>
</table>
1 Answers

You don't need to use data_get you can use Carbon for the date comparison. Simply parse the dates (probably they are already a Carbon instance). Then compare them:

$firstDate = Carbon::parse(transactionsByDate);
$secondDate = Carbon::parse($date);
$first->greaterThan($second); // this will return true or false

Then you can use these comparison helpers.

Related