Alen
Alen

Reputation: 1291

ErrorException in VerifyCsrfToken.php line 156: Trying to get property of non-object laravel middleware

I am getting this error when I created my own middleware and used it

public function handle($request, Closure $next)
{
    $privilege = $request->session()->has('privilege');

    if($request->session()->has('privilege'))
    {
        if($privilege == "Owner" || $privilege == "owner")
        {
            return $next($request);
        }
        else
        {
            return redirect()->back()->withErrors(['privilege_check' => "You are not privileged to go there!."]);
        }
    }
    return '/home';
}

Upvotes: 0

Views: 6010

Answers (2)

ImBhavin95
ImBhavin95

Reputation: 1527

Check this

public function __construct()
{
    $this->middleware(function ($request, $next) {
        if(Session::get('user_id') == NULL)
        {
            return Redirect::to('login');
        }else{
            return $next($request);
        }
    });
}

Upvotes: 3

Ali Rasheed
Ali Rasheed

Reputation: 2817

Most probably you haven't set the session('privilege'). If you have already set it up then close down the browser and run the application again from start. It maybe did not set up during the development.

Upvotes: 3

Related Questions