Home > database >  How to pass a parameter to a laravel controller from redirect within the same controller
How to pass a parameter to a laravel controller from redirect within the same controller

Time:01-07

How do I pass a true false value to the controller from the redirect in the class and to the router and back to another function in the same controller class if that makes sense Like

public function 1() {
    return redirect('route2');
}

public function2() {
    I need to access the variable here that some how gets passed from the first function
}

Because these functions are both on my main controller and I need to pass a variable through the route and back into the controller or is there a way to put a state variable on the class or something I just need to call a function on the controller with conditions from the previous controller function that called called the redirect route.

Also sorry if I am mixing up class and function I am new to laravel and MVC in general.

CodePudding user response:

You can do something like this:

public function first() {
    return redirect()->action(
        [YourController::class, 'second'], ['value' => true]
    );
}

public function second($value = null) {
    
    // whatever you want

}

https://laravel.com/docs/9.x/redirects#redirecting-controller-actions

CodePudding user response:

I think this code help you:

public function 1() {
    return to_route('YOUR_ROUTE_NAME', ['value' => 'some things...']);
}

public function2(Request $request, $value) {
    // Use the value passed as a route parameter
    // $value is 'some things...' 


}
  • Related