mardi 6 avril 2021

Alter the returned value inside a query builder query in Laravel 5.5

I am trying to get a price from my database that is stored as an integer. An example of a value that I am getting is 3999 and I want it returned as $39.99. If the value is in the database NULL I want the value to be returned as "N/A".

What I am doing currently works but it uses a for-loop were I suspect that I could do this in the query itself.

$query = AccesscodeHeader::where('channel_partner_id', '=', $channelPartnerId);
$results = $query->get();

foreach ($results as $key => $result) {
    $result->price = $result->price != null ? "$" . number_format(($result->price/100), 2, '.', '') : "N/A";
    $result->remaining = $result->quantity - $result->used;
}

Am I able to get the value in the desired format in the query itself or is this best done in the for-loop?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire