-1

I have a case where the exact same eloquent builder query is being run twice. This is on purpose to try and figure this out. Basically when the query executes inside of the route middleware it takes over 5000ms to complete. However in the controller where I added the same query it only takes 0.06ms. So my question is why would the exact same query take such a long time in the middleware and not in the controller?

1 Answers1

0

don't see why accessing the database in middleware would be bad practice. Take a permission system for example. Your middleware would have to verify that the logged in user is allowed to view the current page. There's no way to do that without querying the database (except if you'd get the permissions from somewhere else)

If this query probably runs on many requests you should make sure that you optimize it properly and reduce the query time to a minimum.

Ahmed Shams
  • 338
  • 1
  • 9
  • Oh believe me I completely agree. However I the query I am running as a test in this case is select * from users where id = 1. This is being run as User::find(1). This done in the middleware takes around 5000ms but not in the controller method. It’s just odd. – Nicholas Bates Feb 01 '18 at 04:52
  • can you post your middleware or try to make new project and set up only this query in middleware and check bec 5000ms is GREAT TIME for one query – Ahmed Shams Feb 01 '18 at 04:57