Martin
Martin

Reputation: 557

Ternary Operator on a response in Laravel

Am working on a Laravel app whereby I get response of date which has a negative (meaning it was days ago) and plain date (no negative meaning days in the future) e.g -140 days means 140 days ago and 140 days means 140 days to come.

On the view am trying to create a Regex and a ternary operator whereby if the response has a negative before it,,, I should remove the negative sign before and append days ago after it. For instance -140 should change to 140 days ago and 140 should change to 140 days.

Logic

public function allRenewal()
{
    //Fetch response from the API
    $life = GeneralHelper::global_Curl([], 'api/v1/b2b/life/agent-policies')->data;

    //Sort all in descdending order acccording to days left  
    $lifeSort = collect($life)->sortBy('days_left');

    //dd($lifeSort);

    return view('B2B::pages.renewals', ['lifePol' => $lifeSort]);
}

View

@foreach($lifePol as $life_d)
    <tr>
        <td>{{$life_d->days_left}}</td>
    </tr>
@endforeach

Upvotes: 0

Views: 275

Answers (1)

Ijas Ameenudeen
Ijas Ameenudeen

Reputation: 9259

This should work

<td>{{ $life_d->days_left < 0 ? ($life_d->days_left * -1) . ' days ago' : $life_d->days_left  . ' days' }}</td>

Please note $life_d->days_left == 0 is not considered here.

Fiddle : https://implode.io/SW1ib9

Upvotes: 1

Related Questions