RafaelMunoznl
1 month ago

Memory exhausted after so much loops. Back to Eloquent

Posted 1 month ago by RafaelMunoznl

I have been working for a while in a application which turned very slow. The reason is that I have been manipulatig the results of a query to get the data in the needed structure to pass it to the front end. I mean tons of looping and conditionals.

The point is that need to transfor this, that got this from the Database:

Collection {#5810 ▼
  #items: array:1 [▼
    "Marianne" => Collection {#5809 ▼
      #items: array:6 [▼
        0 => Appointment {#4742 ▼
          ...
          #attributes: array:16 [▼
            "id" => 190
            "company_id" => 2
            "employee_id" => 4
            "client_id" => 71
            "offer_id" => 8
            "startDateTime" => "2019-12-04 09:00:00"
            "endDateTime" => "2019-12-04 10:00:00"
            "created_at" => "2019-12-10 19:49:34"
            "updated_at" => "2019-12-10 19:49:34"
          ]
          ....
        }
        1 => Appointment {#4743 ▶}
        ...
        35 => Appointment {#4747 ▶}
      ]
    }
  ]
}

Into this:

array:1 [▼
  "Marianne" => array:5 [▼
    "09:00" => Appointment {#6050 ▼
            ...
          #attributes: array:16 [▼
            "id" => 190
            "company_id" => 2
            "employee_id" => 4
            "client_id" => 71
            "offer_id" => 8
            "startDateTime" => "2019-12-04 09:00:00" // I use this hour "09:00:00" as array key.
            "endDateTime" => "2019-12-04 10:00:00"
            "created_at" => "2019-12-10 19:49:34"
            "updated_at" => "2019-12-10 19:49:34"
            ...
          ]
        ...
    }
    "10:00" => Appointment {#6052 ▶}
    "13:00" => Appointment {#6054 ▶}
    "14:00" => Appointment {#6055 ▶}
    "15:00" => Appointment {#6056 ▶}
  ]
]

Notice that there is not other difference than using the hour in startDateTime as key of the array.

In order to get that hour as key I was looping trough all the appointments of each employee of each company , each day, extracting the hour from the carbon, with several condictionals in between until I have got the error

"memory exhausted".

My second Approach is to try to get that structure directly from the database using MySQL and eEoquent. After making literally hundreds of intends without success, the closest I got is using PLUCK.

I wonder if I could do something like pluck('time', function). And this is my "solution".

        $query = Appointment::join('employees', 'employees.id', 'appointments.employee_id')
            ->join('users', 'users.id', 'employees.user_id')
            ->join('offers', 'offers.id', 'appointments.offer_id')
            ->join('clients', 'clients.id', 'appointments.client_id')
            ->where('appointments.company_id', $companyId)
            ->whereDate('startDateTime', 'like', '%' . $datum . '%')
            ->orderBy('appointments.employee_id')
            ->orderBy('appointments.startDateTime')
            ->pluck('appointments.startDateTime', function($query)
                {
                    select(
                        'appointments.*',
                        'offers.title As offer',
                        'clients.lastname as client',
                        'users.firstname as employee'
                    )
                )->get()->groupBy('employee');

However againn I got an error:

stripos() expects parameter 1 to be string, object given

But this time I have not idea what does that error means, where is the object,

Could anybody tell me what am I doing wrong in that query?

Please sign in or create an account to participate in this conversation.