with("tradings")->latest()->first()->tradings; $last_tradings = $last_tradings->unique('title'); $tradings = []; foreach($last_tradings as $trading){ $title = trim(strtok($trading->title, '(')); $before_last_trading = Trading::where("title",'LIKE', "%{$title}%")->where('group_id', '!=', $trading->group_id)->latest()->first(); $difference = !$before_last_trading ? 0 : $this->getPercentageDifference($trading->price ?? 0, $before_last_trading->price ?? 0); array_push($tradings, (object)[ 'id' => $trading->id, 'title' => $title, 'unit' => $trading->unit, 'price' => $trading->price, 'old_price' => !$before_last_trading ? 0 : $before_last_trading->price, 'price_change' => number_format($difference, 1, ".", "")."%", 'amount' => $trading->amount, 'currency' => $trading->currency, 'seller_country' => $trading->seller_country, 'buyer_country' => $trading->buyer_country, 'point' => $trading->point, ]); } $tradings = Trading::hydrate($tradings); return $this->respondWithCollection($tradings, new TradingTransformer); dd($tradings); } function getPercentageDifference($new, $old){ // return (($new - $old) / $old) * 100; // return (abs(($old - $new)) / ($old + $new) /2); // return (abs($old - $new) / (($old + $new)/ 2)) * 100; // return (abs($old - $new) / (($old + $new)/ 2)) * 100; return (($new - $old) / abs($old)) * 100; } }