How does Laravel middleware work and when should it be used?
I am learning Laravel and I want to understand how middleware works. What is the purpose of middleware in Laravel, how does it process HTTP requests, and in which situations should middleware be used? Please explain with a simple example such as authentication or role-based access.