Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database access in middleware Laravel 5

Tags:

php

laravel-5

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.

like image 705
curious_coder Avatar asked Mar 06 '15 11:03

curious_coder


1 Answers

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.

like image 122
lukasgeiter Avatar answered Oct 14 '22 06:10

lukasgeiter