Home > database >  Is it possible to retrieve a value from a redirect in another class before performing the redirect?
Is it possible to retrieve a value from a redirect in another class before performing the redirect?

Time:02-02

I am currently working with a controller, OnboardingController.php. This controller calls another method in a different class, let's call it OnboardingService.php, so for example:

OnboardingController

public function doThing()
{
    return $this->doAnotherThing();
}

OnboardingService

public function doAnotherThing()
{
    return redirect('/')->with(['propertyA' => 'valueA']);
}

Would I be able to access propertyA in OnboardingController before returning the redirect? And if so, how would I access that property?

e.g.

OnboardingController

public function doThing()
{
    $doAnotherThing = $this->doAnotherThing();
    Log::info($doAnotherThing->propertyA);

    return $doAnotherThing;
}

I am currently using Laravel 6.

CodePudding user response:

You're using redirect(...)->with(...) to flash propertyA, so propertyA should be accessible from the session

public function doThing()
{
    $doAnotherThing = $this->doAnotherThing();
    Log::info(session()->get('propertyA'));

    return $doAnotherThing;
}
  •  Tags:  
  • Related