Creating a session I'm trying to create a session called cart_session and when I print the session I get a blank.
use Session;
class CustomersController extends Controller
{
public function logout(Request $request)
{
$cart_data = Session::get('cart_session');
echo "<PRE>";
print_r($cart_data);
die();
}
}
Here is my full code
public function postLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required',
]);
if(Customer::where('email', '=', Input::get('email'))->exists())
{
if(auth('customer_admin')->attempt(array('email' => $request->input('email'), 'password' => $request->input('password'))))
{
if(auth()->guard('customer_admin')->user()->is_activated == '0')
{
$this->logout();
return back()->with('error', 'Please activate your account');
}
$name = auth()->guard('customer_admin')->user()->name;
$customer_id = auth()->guard('customer_admin')->user()->id;
$carts_tbl = DB::table('cart_session')->where(['customer_id' => $customer_id])->first();
if(!empty($carts_tbl))
{
$cart_data = json_decode($carts_tbl->data);
Session::put('cart_session', $cart_data);
}else{
$cart_data = '';
Session::put('cart_session', $cart_data);
}
return redirect()->route('customer.dashboard');
}else{
return back()->with('error', 'Your email/password combination is wrong');
}
}else{
return back()->with('error', 'Please <a href="'.route('customer.signup').'">sign up</a>');
}
}
public function logout(Request $request)
{
$auth_id = auth()->guard('customer_admin')->user()->id;
$cart_data = Session::get('cart_session');
$cart = [
'data' => json_encode($cart_data),
];
dd($cart);
}
What I'm trying to do here is when a customer logs in the a session is created and then when they log out it gets saved into the database. At the moment I'm just trying to echo out the data to make sure I'm getting the right thing.
As I can see, the cart_session is not set when you call the logout method.
You try to access it before you set it.
It should be set in log in. Doesn't this set it
Session::put('cart_session', $cart_data);
Yes you are right but as you can see:
if(auth()->guard('customer_admin')->user()->is_activated == '0')
{
$this->logout();
return back()->with('error', 'Please activate your account');
}
Here it is where you call the logout. At this time the session is not set. Maybe what you can try is:
Open tinker
Session::put('car_session', 'test data');
Try to login again, you should have something in this session key.
Please sign in or create an account to participate in this conversation.