I am experimenting with Laravel 5 and came across HTTP Middleware. I am curious to know whether it is advisable to access database (checking) from middleware and filter HTTP requests as per the database outcome?
P.S I was able to perform database queries in middleware.
I 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With