Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

wim91's avatar

Is it possible to make a redirect with a set of data?

Hello everyone, maybe this is an old or repeated question, I apologize in advance, but I couldn't find the answer to it myself. Since I'm just starting to write in Laravel, I have problems that I'm trying to solve.

In the controller, after sending the form, I get data from several models, compose them into an array, and also create new arrays based on the received data...

Once I have all the data, I need to navigate to the page where this data will be inserted into the blade template.

How do I pass a set of values to the page?

Problem:

  1. If I use return view('result', compact(['arr1', 'arr2', 'arr3'...])); then when I refresh the page, the form is resubmitted and all the logic is re-executed, which I don't want.
  2. If I do return redirect()->route('resultName'); then the data is not saved, and adding through the session ->with('arr1', $arr1)->with('arr2', $arr2)... doesn't seem very good to me, if there is large data, plus you need to convert arrays to strings and back. I would also like this data not to be stored for 120 minutes while the session is alive, but to be available only on a specific page.

How can I solve such problems?

0 likes
6 replies
JussiMannisto's avatar

and adding through the session ->with('arr1', $arr1)->with('arr2', $arr2)... doesn't seem very good to me, if there is large data, plus you need to convert arrays to strings and back.

Why doesn't it seem good to you? The data must be kept somewhere, and flashing it in session is certainly better than sending it to the client, sending it back to the server, validating it, then passing it to the view...

And why would you need to convert arrays to strings? Laravel handles serialization, so you don't need need to do anything. You can store arrays, objects, etc.

In short: use with(). It exists for this exact purpose. You don't need multiple with() calls: you can pass keys and values as an associative array:

redirect()->route('foo')->with([
	'arr1' => $arr1,
	'arr2' => $arr2,
]);
1 like
wim91's avatar

@JussiMannisto Yes, this is a working method, I just decided to ask more knowledgeable people. And I don't want to store in sessions because of a possible simultaneous session, for example, the client will fill out a form in 2 browser tabs at once, when saving sessions, it will overwrite the contents of the last tab and this can cause a problem with data processing. But I may be wrong. Regarding arrays, when I tried this, I got a data type error that a string was expected, not an array. They will test it again, maybe I changed something else somewhere.

And another question, is it normal that an array will be stored in the session, and possibly quite large? Is it possible to somehow change the session lifetime at the creation level?

JussiMannisto's avatar

@wim91 Storing arrays into the session is completely normal. I don't know what you consider quite large in this case, and the session driver makes a difference, but using the session is certainly better than sending the data back and forth for no reason. The array will be loaded in memory in both cases. The session is faster and more efficient since the data isn't transmitted over the wire multiple times, and you don't have to re-validate it.

When you call with(), the data is flashed to the session, meaning it will only be stored until the next request comes in. Since you're doing a redirection, the data will be pulled when the redirection completes. That means there won't be any conflicts even if the user has multiple tabs open: the data is only kept for the duration of the redirect.

jaseofspades88's avatar

Typically a form should be submitted as a POST request where you persist any data you later rely on to the database, etc. Once completed redirect to a route using an either slug or an id in the url and then use the redirected to controller to fetch, format and render the relevant views to the user. This is a common and frequently used pattern..

I would recommend you don't rely on the session to persist important information as this is easily lost and/or manipulated.

A simple example would be...

	Route::post('product', [ProductController::class, 'store']); //This route is where you send the form to
	Route::get('product/{product}', [ProductController::class, 'show']); //This route is where you send the user to after persisting

Please or to participate in this conversation.