php - Laravel session data empty after user login
Get the solution ↓↓↓I am using laravel version 6.17.0.
If I put data to sessionSession::put('cart_merge', $data)
inauthenticated()
method ofAuthenticatesUsers
trait and I try to get them withSession::get('cart_merge')
in Controller, the session is empty. If I doSession::get('cart_merge')
right after insertion inauthenticated()
method, they are still there.
I tried grouping routes as adviced in https://stackoverflow.com/a/35140419/5821265 but it didn't help.
Why do session doesn't make it untill Controller?
AuthenticatesUsers.php:
protected function authenticated(Request $request, $user)
{
$cart = getCart::getCart();
$savedCart = $user->cart;
if (empty($cart['cart'])){ // Shopping cart is empty
$cart = [];
foreach ($savedCart as $item) {
$product = Product::where('id', $item->id)->where('visibility', 1)->where('stock', '>', 0)->first();
if (is_null($product)) {
continue;
}
if ($item->pivot->amount > $product->stock){
$cart[$item->id]['amount'] = $product->stock;
}
else {
$cart[$item->id]['amount'] = $item->pivot->amount;
}
$cart[$item->id]['name'] = $product->name;
$cart[$item->id]['url'] = $product->url;
$cart[$item->id]['image'] = $product->image;
}
Session::put('cart', $cart);
}
else { // There are some items in shopping cart.
if ($savedCart->isEmpty()){ // User doesn't have any items saved in DB from last session.
foreach ($cart['cart'] as $item_id => $item){
DB::table('product_user')->insert([
'user_id' => $user->id,
'product_id' => $item_id,
'amount' => $cart['cart'][$item_id]['amount'],
'type' => 0
]);
}
}
else { // User has saved items in DB from last session.
Session::put('cart_merge', $savedCart->toArray());
// if I do Session::get('cart_merge') here, the data are correct.
}
}
}
SystemController.php:
class SystemController extends Controller
{
public function index(){
$data = Session:get('cart_merge');
dd($data);
// if I do Session::get('cart_merge') here, null is returned instead of data.
}
}
web.php:
Route::group(['middleware' => ['web']], function () {
Route::get('/', 'SystemController@index');
Auth::routes();
});
Answer
Solution:
You are accessing session in just in class directly
Session is depend on Request.
So might be your request method will route to some controller method and then u can access the session. otherwise how laravel will know whose session it is
Ex
Route::group(['middleware' => 'web'], function () {
Route::get('', function () {
Session::set('test', 'testing');
});
Route::get('other', function () {
dd(Session::get('test'));
});
});
check this at https://stackoverflow.com/a/35140419/4153682
Answer
Solution:
Fixed by changing session driver toSESSION_DRIVER=file
instead ofcookies
in.env
file. It seems like cookies option isn't persistent.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: integrity constraint violation: 1452 cannot add or update a child row: a foreign key constraint fails
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.