skip to Main Content

how to make a global array variable in laravel controller

i tried this

class AuthController extends Controller
{
    public $loginCred = [];

    public function postLogin(Request $request){
        $request['email'] = $loginCred['email'];
        $request['password'] = $loginCred['password'];
        // a bunch of code later
        return redirect()->away($authUrl);
    }

    public function callback(Request $request)
    {
        $loginCred->session()->regenerate();
        return redirect()->route('home');
    }
}

but it gives ErrorException "Undefined variable $loginCred"

2

Answers


  1. Chosen as BEST ANSWER

    If the defining process is reversed like this:

    $loginCred['email'] = $request['email'];
    $loginCred['password'] = $request['password'];
    

    It works on my machine, the login passed, and the error is gone.


  2. If you define a variable in a controller, your functions need to use $this->variable to access it.
    Therefore, you should use:

    $this->loginCred
    

    to access your $loginCred variable.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search