Skip to content
Advertisement

Laravel Middleware not executing functions

I have this Locale middleware which sets language based from settings on DB

JavaScript

but i need to get the branch where the current user is so i need to get the Auth::id() first so i changed my code to this to access Auth.

JavaScript

Now i am getting the current user logged in but the problem is it is not executing this block of code

JavaScript

Kernel.php

JavaScript

Note: the code wihout Auth is executing this block of code but the second isn’t

Am i missing something here?

Advertisement

Answer

For the second one you have

JavaScript

at the very beginning, so the code will execute at response.

You need to do something like this:

JavaScript

If you do not have the auth()->user(), this is because the place that you register the middleware is not correct.

The global middleware stack runs prior to the session being started and authentication details being available.

Define this at the bottom of the ‘web’ group or in your route middleware.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement