Fetch servers with product to reduce SQL-Queries

This commit is contained in:
BuildTools 2022-06-04 11:39:23 +02:00
parent 0f5369043d
commit f6eab834fc

View file

@ -233,14 +233,18 @@ class User extends Authenticatable implements MustVerifyEmail
*/ */
public function creditUsage() public function creditUsage()
{ {
$usage = 0; $servers = Server::query()
->where('user_id', '=', $this->id)
->with('product')
->get();
foreach ($this->Servers as $server) { $usage = 0;
foreach ($servers as $server) {
$usage += $server->product->price; $usage += $server->product->price;
} }
return number_format($usage, 2, '.', ''); return number_format($usage, 2, '.', '');
} }
/** /**
* @return array|string|string[] * @return array|string|string[]