How to return redirect with compact variable in laravel?

Member

by rollin , in category: Third Party Scripts , 2 months ago

How to return redirect with compact variable in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by brandy , 2 months ago

@rollin 

To return a redirect with a compact variable in Laravel, you can use the redirect() method along with the with() method. Here's an example:

1
return redirect()->route('target.route')->with(compact('variable1', 'variable2'));


In this example, target.route is the route you want to redirect to, and variable1 and variable2 are the compact variables that you want to pass along with the redirect. Laravel's with() method accepts an array of key-value pairs, so you can pass as many variables as you need.


When the redirect is triggered, the compact variables will be flashed to the session for the next request and can be accessed in the redirected route or view using the session() helper function.


Note: Make sure to replace 'target.route' with the actual route name or URL you want to redirect to.