1

My middleware code
php version 5.6.32-1

<?php

namespace App\Http\Middleware;
use Session, Closure;

class AuthLogin
{
    public function handle($request, Closure $next)
    {
        if (!Session::has('account')) {
          return redirect('/admin/login');
        }

        return $next($request);
    }
}

And I get this error

array_key_exists(): The first argument should be either a string or an integer

what's happend with it?

There is not any error on my mac, but it's happen on my linode server

2
  • clear cache this is a trial Commented Jan 5, 2018 at 18:24
  • What version of Laravel? 5.5 requires 7.0+ Commented Jan 5, 2018 at 18:50

2 Answers 2

1

You are getting this error due to the following reason :

$arr = [[0] => 10,[1] => 20,[2] => 30,[3] => 40,[4] => 50];

let's say you search for values 10 or 30

$k = array_search('10', $arr);

now $k will hold [0]

and if do this

array_key_exists($k, $arr);

you will get array_key_exists(): The first argument should be either a string or an integer because $k i,e 0 is treated as boolean not string or int but if we have searched 30 then this issue will not occur.

Sign up to request clarification or add additional context in comments.

Comments

0

For anyone who facing this problem, I solved this by clearing config cache using:

 php artisan config:clear

or also you can using command (this will clear all cache in your application, including route, config, view and app cache):

php artisan optimize:clear

And this is how I check if session exist:

 public function handle($request, Closure $next)
    {
        if (!$request->session()->exists('session_name')) {
            return redirect('/');
        }

        return $next($request);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.