jeudi 27 août 2020

Laravel Middleware not executing functions

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

    public function handle($request, Closure $next)
    {
        $HQ = Branch::where('is_hq', 1)->where('is_active', 1)->first();

        $Company = GlobalVariable()->branch($HQ)->all()->whereIn('group', array(1, 3, 9))->keyBy('key');
        $locale = strtolower($Company['bi__language']->value);

        if ($locale === 'eng') {
            \App::setlocale('en');
        } else {
            \App::setlocale($locale);
        }

        return $next($request);
    }

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.

     public function handle($request, Closure $next)
     {
        $response = $next($request);

        $HQ = Branch::where('is_hq', 1)->where('is_active', 1)->where('user_id', \Auth::id())->first();

        $Company = GlobalVariable()->branch($HQ)->all()->whereIn('group', array(1, 3, 9))->keyBy('key');
        $locale = strtolower($Company['bi__language']->value);

        if ($locale === 'eng') {
            \App::setlocale('en');
        } else {
            \App::setlocale($locale);
        }

        return $response;
    }

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

if ($locale === 'eng') {
   \App::setlocale('en');
} else {
    \App::setlocale($locale);
}

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

Am i missing something here?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3b20Aay
via IFTTT

Aucun commentaire:

Enregistrer un commentaire