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

grgbikash05's avatar

sending data throught redirect

I want to send the data throught redirect.

I know I can send it through session but when the page refreshes it will get lost.

So a good way of sending data thorugh redirect is???

0 likes
12 replies
Snapey's avatar

The only way you can send data through a redirect is by adding it to the querystring

Session persists through page refresh - otherwise there would not be a lot of point in it. Think about sites that persist a shopping basket for instance.

Snapey's avatar

If you want to 'send' a variable to another page with redirect;

/myotherroute?data=fred&moredata=doe

or

put the data in session.

but when the page refreshes it will get lost.

Wrong

grgbikash05's avatar

/myotherroute?data=fred&moredata=doe this method seems good to mee

grgbikash05's avatar

@snapey

In my first controller I store the data and redirect to the 'questions' like this and $id is the parameter i want to send..

return redirect()->route('questions', ['id' => $id]);

In my web.php

Route::get('/questions/{$id}', 'QuestionsController@id')->name('questions');

In my QuestionsController,

public function id($id) {
    echo $id;
    }

I also get the url

http://127.0.0.1:8000/questions/2

But It always says page not found... why??

nnnayeem's avatar

You can return view and send data with it.

grgbikash05's avatar

@nnnayeem I don't want to use view because the next view i want to pass will have the same route

Vilfago's avatar

Do you have another route named "questions" ?

Or that I have an url similar to "/questions/" ?

grgbikash05's avatar

@Vilfago This is all my routes

Route::get('/', function () {
        return view('index');
});

Route::post('/submit_survey', 'SurveysController@store'-    >name('submit_survey');

Route::get('/submit_survey', 'SurveysController@index')->name('submit_survey');

Route::post('/submit_categories', 'CategoriesController@store')->name('submit_categories');

Route::get('/submit_categories', 'CategoriesController@index')->name('submit_categories');

Route::get('/questions/{$id}', 'QuestionsController@id')->name('questions');

Route::post('/submit_no_of_questions', 'CountQuestionsController@store')->name('submit_no_of_questions');

Route::get('/submit_no_of_questions', 'CountQuestionsController@index')->name('submit_no_of_questions');

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
Snapey's avatar

remove the $ from id in the route

not Route::get('/questions/{$id} but instead Route::get('/questions/{id}

Please or to participate in this conversation.