Reputation: 229
I have below the array.
$data = array(
'category'=>array(
'0'=>1,
'1'=>15,
'2'=>7,
'3'=>76,
)
)
How do i store above array into cookie in laravel 5.6 ? and how to retrive all cookies value from cookie?
Upvotes: 0
Views: 3587
Reputation: 2683
You need to serialize array with json_encode or serialize functions and store it with Cookie facade.
Cookie::queue('cookie_name', json_encode($data), $cookieTime);
or
Cookie::queue(Cookie::make('cookie_name', json_encode($data), $cookieTime));
Upvotes: 4
Reputation: 640
setCookieArray( $arr, $cookie, $minutes ) {
$json = serialize( $arr );
if( strlen( $json ) > 4096 ){
foreach( $arr as $key => $val ){
if( is_array( $val ))
setCookieArray( $val, $cookie .'_a_'. $key, $minutes )
else
cookie( $cookie .'_v_'. $key, $val, $minutes);
}
} else {
cookie('name', $json, $minutes);
}
}
setCookieArray( array(
'category'=>array(
'0'=>1,
'1'=>15,
'2'=>7,
'3'=>76,
)
), 'mycookie', 10);
Upvotes: 0
Reputation: 367
$data = array(
'category'=>array(
'0'=>1,
'1'=>15,
'2'=>7,
'3'=>76,
)
);
// to store
$json = serialize($data); // convert to string
cookie('name', $json, $minutes);
// to retrive
$value = Cookie::get('name');
Upvotes: 0