Explorar o código

Merge pull request #434 from Tobiletsmc/development

Increase Controlpanel performance with fetch servers with product to reduce SQL-Queries
Dennis %!s(int64=3) %!d(string=hai) anos
pai
achega
3d1cf9116b
Modificáronse 1 ficheiros con 9 adicións e 4 borrados
  1. 9 4
      app/Models/User.php

+ 9 - 4
app/Models/User.php

@@ -196,7 +196,7 @@ class User extends Authenticatable implements MustVerifyEmail
      */
     public function unSuspend()
     {
-        foreach ($this->servers as $server) {
+        foreach ($this->getServersWithProduct() as $server) {
             if ($this->credits >= $server->product->getHourlyPrice()) {
                 $server->unSuspend();
             }
@@ -233,14 +233,19 @@ class User extends Authenticatable implements MustVerifyEmail
      * @return string
      */
     public function creditUsage()
-    {
+    {            
         $usage = 0;
-
-        foreach ($this->Servers as $server) {
+        foreach ($this->getServersWithProduct() as $server) {
             $usage += $server->product->price;
         }
 
         return number_format($usage, 2, '.', '');
+    }    
+
+    private function getServersWithProduct() {
+        return $this->servers()
+            ->with('product')
+            ->get();
     }
 
     /**