boomdrak boomdrak - 1 year ago 235
PHP Question

Laravel session data get cleared out after redirect to next page

Using Laravel 5.2

Im trying to store session data in controller, then if validation succeed, i want to redirect the user to next page. When i do the Redirct::to('nextpage') the session data is lost in my next page and controller.

This is the controller in getting the form post value from 'domainSearch'

class domainSearch extends Controller
public function store(Request $request)
// validate the info, create rules for the inputs
$rules = array(
'domainSearch' => 'required' // make sure the username field is not empty

// run the validation rules on the inputs from the form
$validator = Validator::make(Input::all(), $rules);

// if the validator fails, redirect back to the form
if ($validator->fails()) {
return Redirect::to('/')
->withErrors($validator) // send back all errors to the login form
->withInput(Input::except('password')); // send back the input (not the password) so that we can repopulate the form
else {
// validation not successful, send back to form
$request->session()->put('domainname', $request->get('domainSearch'));
return Redirect::to('/domainlist');



This is the controller im trying to pass session data to and pick up. But it always remain null.

class DomainList extends Controller
public function index(Request $request){

return view('domainlist');

If i send the user to next page with return view('view'); the session data is there, but get cleared out when using the redirect function.

How to i redirect without losing session data and variables?

Answer Source

try var_dump($request->session()->get('domainname')); in your index.

BTW, you can also use the session() global function to store or retrieve your session data.

Route::get('home', function () {
  // Retrieve a piece of data from the session...
  $value = session('key');

  // Store a piece of data in the session...
  session(['key' => 'value']);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download