Laravel session cannot update on ajax request

3.06K views
1 Comment
28 Aug 2015 12:50 am

Sometimes laravel set the new or update session in ajax requestion even you can print it like Session::all(); it will print the updated session but cannot update the session on fresh page, for eg. if a user update his information like display name picture or other information through ajax.

In this things have two major causes the first is use die in ajax and second thing is echo success message or something else, You must need to use return instead of echo or die. Here is I am showing  both wrong ways

Wrong with DIE

if($update_userdata===true){
 Session::put('userprofile_data',$update_userdata);
 die('success:user data updated');
}
die('error: something went wrong, try again');
//THIS IS WRONG IT CANNOT UPDATE AND BRING OLD SESSION DATA ON PAGE RELOAD

Wrong with ECHO

if($update_userdata===true){
 Session::put('userprofile_data',$update_userdata);
 echo 'success:user data updated';
}
echo 'error: something went wrong, try again
//THIS IS WRONG IT CANNOT UPDATE AND BRING OLD SESSION DATA ON PAGE RELOAD

Both above are wrong because headers set at this when you use echo string and if you use die it kill/exit script.

Right way to update session data

Use return won’t worry it cannot only return data this will show message and update the session data on reloading the page.

if($update_userdata===true){
 Session::put('userprofile_data',$update_userdata);
 return 'success:user data updated';
}
return 'error: something went wrong, try again
//THIS IS WRONG IT CANNOT UPDATE AND BRING OLD SESSION DATA ON PAGE RELOAD

NOTE:Your Email Address will be not shown and please do not add spamming comments because here is REL="NOFOLLOW" on your links and comments also moderated shown.
<code>Put html css or any language code under this tag</code>

1 Comments on Laravel session cannot update on ajax request