how to put data to laravel session uing array of array?











up vote
-1
down vote

favorite












I am trying to make a cart and have the following requirements :
1.when user click adds to cart product added to the session like this:



    Array
(
[items] => Array
(
[0] => Array
(
[product_id] => 15
[addtocart_totalmrp] => 8953
[addtocart_totalsellingprice] => 7876
[addtocart_auid] => 7065bec17a101cf2,3025bec186520de3,6725bec18d7889a7
)

[1] => Array
(
[product_id] => 39
[addtocart_totalmrp] => 12654
[addtocart_totalsellingprice] => 11876
[addtocart_auid] => 1675bed115132bc6,755bed11033fe7d
)

),
[2] => Array////which is not coming after 2 it stops and start overwriting the 2nd position
(
[product_id] => 39
[addtocart_totalmrp] => 12654
[addtocart_totalsellingprice] => 11876
[addtocart_auid] => 1675bed115132bc6,755bed11033fe7d
)

)

)


here is my code:



$cart1 = array("product_id" => $request->product_id,
"addtocart_totalmrp" => $request->addtocart_totalmrp,
"addtocart_totalsellingprice" => $request->addtocart_totalsellingprice,
"addtocart_auid" => $request->addtocart_auid);


if ($request->session()->has('cart')) {

$products = Session::get('cart.items', array()); // get existed products or empty array

$products["items"] = $cart1; // add new product to list
echo'<pre>';
print_r($products);


Session::push('cart.items', $products); //putting that data to cart

print_r(count($products));
dd($products);

} else {
echo 'session haas no data';
$products["items"] = $cart1; // add new product to list
print_r($products);
Session::put('cart.items', $products);

}


my problem:
when I try to add a third product it is overwriting the 2nd one don't know how to do it.



I have tried this code and its working but there is item key I used once coming again any suggestion for this here is the array structure:



array:1 [
"items" => array:4 [
"items" => array:1 [
0 => array:4 [
"product_id" => "39"
"addtocart_totalmrp" => "12654"
"addtocart_totalsellingprice" => "11876"
"addtocart_auid" => "1675bed115132bc6,755bed11033fe7d"
]
]
0 => array:4 [
"product_id" => "28"
"addtocart_totalmrp" => "17145"
"addtocart_totalsellingprice" => "12165"
"addtocart_auid" => "5205bed102b6d856,8255bed103e3ce14"
]
1 => array:4 [
"product_id" => "15"
"addtocart_totalmrp" => "8953"
"addtocart_totalsellingprice" => "7876"
"addtocart_auid" => "7065bec17a101cf2,3025bec186520de3,6725bec18d7889a7"
]
2 => array:4 [
"product_id" => "15"
"addtocart_totalmrp" => "8953"
"addtocart_totalsellingprice" => "7876"
"addtocart_auid" => "7065bec17a101cf2,3025bec186520de3,6725bec18d7889a7"
]
]
]


my new code



if ($request->session()->has('cart')) {

// $productsfromsession = Session::get('cart', array()); // get existed products or empty array
// dd($productsfromsession);
// $products["items"] = $cart1; // add new product to list
// echo'<pre>';
// print_r($products);



Session::push('cart.items',$cart1); //putting that data to cart
$a=Session::get('cart', array());
//print_r(count($products));
dd($a);

} else {
echo 'session haas no data <pre>';
$products["items"] = $cart1; // add new product to list
print_r($products);
Session::put('cart.items', $products);

}


New UPdate of code as per answer:



$cart1 = array("product_id" => $request->product_id,
"addtocart_totalmrp" => $request->addtocart_totalmrp,
"addtocart_totalsellingprice" => $request->addtocart_totalsellingprice,
"addtocart_auid" => $request->addtocart_auid);



if ($request->session()->has('cart')) {




Session::push('cart.items',$cart1); //putting that data to cart
$a=Session::get('cart', array());
//print_r(count($products));
dd($a);

} else {
echo 'session haas no data <pre>';
$products = [ $cart1 ]; // create a new cart and add a product in it
Session::put('cart.items', $products);
print_r($products);
}


code work perfectly but the issue of replacing started again something missing in my code?



here is what i want my code is this a sper asked in answers:



public function addtocart(Request $request) {


// if user logged in check login and add data directly to cart without session
if (Auth::check()) {
$user = Auth::user();
echo 'auth sucess';
// adding data to table directly //
} else {

// adding data to cart with session

$cart1 = array("product_id" => $request->product_id,
"addtocart_totalmrp" => $request->addtocart_totalmrp,
"addtocart_totalsellingprice" => $request->addtocart_totalsellingprice,
"addtocart_auid" => $request->addtocart_auid);
print_r('am current cart data : <pre>');
print_r($cart1);

// checking for session has cart or not

if (Session::has('cartdata')) {

echo ' session cart has data ';
} else {

echo 'session has no data add first data to session ';
}
}
}

}


I have tried a new concept of JSON structure can u plz help me where am wrong whenever I try to put data on to array it goes and stop pushing array after 2nd position
here is my Code:



// adding data to cart with session
$cartdata = array("product_id" => $request->product_id,
"addtocart_totalmrp" => $request->addtocart_totalmrp,
"addtocart_totalsellingprice" => $request->addtocart_totalsellingprice,
"addtocart_auid" => $request->addtocart_auid);
// print_r('am current cart data : <pre>');
//print_r($cartdata);


// checking for session has cart or not

if (Session::has('cartdata')) {

// dd(Session::get('cartdata'));

echo ' session cart has new data and it is :<pre> ';

//// fethcing old data from session
$productsfromsession = Session::get('cartdata'); // get existed array value


// decoding old data to normal array
$data=json_decode($productsfromsession);


//dd($data);


// pushing new data to old product array

array_push($data->products,$cartdata);


// saving new data and encoding it again
$newdata=json_encode($data);

dd($newdata); ///every time i add data to this array its stop after 2nd inserted
// removing old data from session
$request->session()->forget('cartdata');


// putting new data to session
Session::put('cartdata',$newdata);

// printing result
dd(Session::get('cartdata'));

//// printng current cart data after pusing new data


} else {


echo 'session has no data adding first data to session is <pre>';

// // putting first item to array
$products=$cartdata;
$cartdata=array("total_price"=>10,"products"=>$products);
$cartdata=json_encode($cartdata);
Session::put('cartdata', $cartdata);

$data = Session::all();
print_r($data);
}









share|improve this question




















  • 1




    use Session::put('cart.items',$products["items"])
    – Leena Patel
    Nov 22 at 6:27










  • thanks for your answer @leena Patel i will check it within a minute
    – Jagdish Sharma
    Nov 22 at 6:28












  • still not working for the third item it is still replacing same. any suggestion?
    – Jagdish Sharma
    Nov 22 at 6:33










  • The values are same may be that is why.
    – Leena Patel
    Nov 22 at 6:37















up vote
-1
down vote

favorite












I am trying to make a cart and have the following requirements :
1.when user click adds to cart product added to the session like this:



    Array
(
[items] => Array
(
[0] => Array
(
[product_id] => 15
[addtocart_totalmrp] => 8953
[addtocart_totalsellingprice] => 7876
[addtocart_auid] => 7065bec17a101cf2,3025bec186520de3,6725bec18d7889a7
)

[1] => Array
(
[product_id] => 39
[addtocart_totalmrp] => 12654
[addtocart_totalsellingprice] => 11876
[addtocart_auid] => 1675bed115132bc6,755bed11033fe7d
)

),
[2] => Array////which is not coming after 2 it stops and start overwriting the 2nd position
(
[product_id] => 39
[addtocart_totalmrp] => 12654
[addtocart_totalsellingprice] => 11876
[addtocart_auid] => 1675bed115132bc6,755bed11033fe7d
)

)

)


here is my code:



$cart1 = array("product_id" => $request->product_id,
"addtocart_totalmrp" => $request->addtocart_totalmrp,
"addtocart_totalsellingprice" => $request->addtocart_totalsellingprice,
"addtocart_auid" => $request->addtocart_auid);


if ($request->session()->has('cart')) {

$products = Session::get('cart.items', array()); // get existed products or empty array

$products["items"] = $cart1; // add new product to list
echo'<pre>';
print_r($products);


Session::push('cart.items', $products); //putting that data to cart

print_r(count($products));
dd($products);

} else {
echo 'session haas no data';
$products["items"] = $cart1; // add new product to list
print_r($products);
Session::put('cart.items', $products);

}


my problem:
when I try to add a third product it is overwriting the 2nd one don't know how to do it.



I have tried this code and its working but there is item key I used once coming again any suggestion for this here is the array structure:



array:1 [
"items" => array:4 [
"items" => array:1 [
0 => array:4 [
"product_id" => "39"
"addtocart_totalmrp" => "12654"
"addtocart_totalsellingprice" => "11876"
"addtocart_auid" => "1675bed115132bc6,755bed11033fe7d"
]
]
0 => array:4 [
"product_id" => "28"
"addtocart_totalmrp" => "17145"
"addtocart_totalsellingprice" => "12165"
"addtocart_auid" => "5205bed102b6d856,8255bed103e3ce14"
]
1 => array:4 [
"product_id" => "15"
"addtocart_totalmrp" => "8953"
"addtocart_totalsellingprice" => "7876"
"addtocart_auid" => "7065bec17a101cf2,3025bec186520de3,6725bec18d7889a7"
]
2 => array:4 [
"product_id" => "15"
"addtocart_totalmrp" => "8953"
"addtocart_totalsellingprice" => "7876"
"addtocart_auid" => "7065bec17a101cf2,3025bec186520de3,6725bec18d7889a7"
]
]
]


my new code



if ($request->session()->has('cart')) {

// $productsfromsession = Session::get('cart', array()); // get existed products or empty array
// dd($productsfromsession);
// $products["items"] = $cart1; // add new product to list
// echo'<pre>';
// print_r($products);



Session::push('cart.items',$cart1); //putting that data to cart
$a=Session::get('cart', array());
//print_r(count($products));
dd($a);

} else {
echo 'session haas no data <pre>';
$products["items"] = $cart1; // add new product to list
print_r($products);
Session::put('cart.items', $products);

}


New UPdate of code as per answer:



$cart1 = array("product_id" => $request->product_id,
"addtocart_totalmrp" => $request->addtocart_totalmrp,
"addtocart_totalsellingprice" => $request->addtocart_totalsellingprice,
"addtocart_auid" => $request->addtocart_auid);



if ($request->session()->has('cart')) {




Session::push('cart.items',$cart1); //putting that data to cart
$a=Session::get('cart', array());
//print_r(count($products));
dd($a);

} else {
echo 'session haas no data <pre>';
$products = [ $cart1 ]; // create a new cart and add a product in it
Session::put('cart.items', $products);
print_r($products);
}


code work perfectly but the issue of replacing started again something missing in my code?



here is what i want my code is this a sper asked in answers:



public function addtocart(Request $request) {


// if user logged in check login and add data directly to cart without session
if (Auth::check()) {
$user = Auth::user();
echo 'auth sucess';
// adding data to table directly //
} else {

// adding data to cart with session

$cart1 = array("product_id" => $request->product_id,
"addtocart_totalmrp" => $request->addtocart_totalmrp,
"addtocart_totalsellingprice" => $request->addtocart_totalsellingprice,
"addtocart_auid" => $request->addtocart_auid);
print_r('am current cart data : <pre>');
print_r($cart1);

// checking for session has cart or not

if (Session::has('cartdata')) {

echo ' session cart has data ';
} else {

echo 'session has no data add first data to session ';
}
}
}

}


I have tried a new concept of JSON structure can u plz help me where am wrong whenever I try to put data on to array it goes and stop pushing array after 2nd position
here is my Code:



// adding data to cart with session
$cartdata = array("product_id" => $request->product_id,
"addtocart_totalmrp" => $request->addtocart_totalmrp,
"addtocart_totalsellingprice" => $request->addtocart_totalsellingprice,
"addtocart_auid" => $request->addtocart_auid);
// print_r('am current cart data : <pre>');
//print_r($cartdata);


// checking for session has cart or not

if (Session::has('cartdata')) {

// dd(Session::get('cartdata'));

echo ' session cart has new data and it is :<pre> ';

//// fethcing old data from session
$productsfromsession = Session::get('cartdata'); // get existed array value


// decoding old data to normal array
$data=json_decode($productsfromsession);


//dd($data);


// pushing new data to old product array

array_push($data->products,$cartdata);


// saving new data and encoding it again
$newdata=json_encode($data);

dd($newdata); ///every time i add data to this array its stop after 2nd inserted
// removing old data from session
$request->session()->forget('cartdata');


// putting new data to session
Session::put('cartdata',$newdata);

// printing result
dd(Session::get('cartdata'));

//// printng current cart data after pusing new data


} else {


echo 'session has no data adding first data to session is <pre>';

// // putting first item to array
$products=$cartdata;
$cartdata=array("total_price"=>10,"products"=>$products);
$cartdata=json_encode($cartdata);
Session::put('cartdata', $cartdata);

$data = Session::all();
print_r($data);
}









share|improve this question




















  • 1




    use Session::put('cart.items',$products["items"])
    – Leena Patel
    Nov 22 at 6:27










  • thanks for your answer @leena Patel i will check it within a minute
    – Jagdish Sharma
    Nov 22 at 6:28












  • still not working for the third item it is still replacing same. any suggestion?
    – Jagdish Sharma
    Nov 22 at 6:33










  • The values are same may be that is why.
    – Leena Patel
    Nov 22 at 6:37













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I am trying to make a cart and have the following requirements :
1.when user click adds to cart product added to the session like this:



    Array
(
[items] => Array
(
[0] => Array
(
[product_id] => 15
[addtocart_totalmrp] => 8953
[addtocart_totalsellingprice] => 7876
[addtocart_auid] => 7065bec17a101cf2,3025bec186520de3,6725bec18d7889a7
)

[1] => Array
(
[product_id] => 39
[addtocart_totalmrp] => 12654
[addtocart_totalsellingprice] => 11876
[addtocart_auid] => 1675bed115132bc6,755bed11033fe7d
)

),
[2] => Array////which is not coming after 2 it stops and start overwriting the 2nd position
(
[product_id] => 39
[addtocart_totalmrp] => 12654
[addtocart_totalsellingprice] => 11876
[addtocart_auid] => 1675bed115132bc6,755bed11033fe7d
)

)

)


here is my code:



$cart1 = array("product_id" => $request->product_id,
"addtocart_totalmrp" => $request->addtocart_totalmrp,
"addtocart_totalsellingprice" => $request->addtocart_totalsellingprice,
"addtocart_auid" => $request->addtocart_auid);


if ($request->session()->has('cart')) {

$products = Session::get('cart.items', array()); // get existed products or empty array

$products["items"] = $cart1; // add new product to list
echo'<pre>';
print_r($products);


Session::push('cart.items', $products); //putting that data to cart

print_r(count($products));
dd($products);

} else {
echo 'session haas no data';
$products["items"] = $cart1; // add new product to list
print_r($products);
Session::put('cart.items', $products);

}


my problem:
when I try to add a third product it is overwriting the 2nd one don't know how to do it.



I have tried this code and its working but there is item key I used once coming again any suggestion for this here is the array structure:



array:1 [
"items" => array:4 [
"items" => array:1 [
0 => array:4 [
"product_id" => "39"
"addtocart_totalmrp" => "12654"
"addtocart_totalsellingprice" => "11876"
"addtocart_auid" => "1675bed115132bc6,755bed11033fe7d"
]
]
0 => array:4 [
"product_id" => "28"
"addtocart_totalmrp" => "17145"
"addtocart_totalsellingprice" => "12165"
"addtocart_auid" => "5205bed102b6d856,8255bed103e3ce14"
]
1 => array:4 [
"product_id" => "15"
"addtocart_totalmrp" => "8953"
"addtocart_totalsellingprice" => "7876"
"addtocart_auid" => "7065bec17a101cf2,3025bec186520de3,6725bec18d7889a7"
]
2 => array:4 [
"product_id" => "15"
"addtocart_totalmrp" => "8953"
"addtocart_totalsellingprice" => "7876"
"addtocart_auid" => "7065bec17a101cf2,3025bec186520de3,6725bec18d7889a7"
]
]
]


my new code



if ($request->session()->has('cart')) {

// $productsfromsession = Session::get('cart', array()); // get existed products or empty array
// dd($productsfromsession);
// $products["items"] = $cart1; // add new product to list
// echo'<pre>';
// print_r($products);



Session::push('cart.items',$cart1); //putting that data to cart
$a=Session::get('cart', array());
//print_r(count($products));
dd($a);

} else {
echo 'session haas no data <pre>';
$products["items"] = $cart1; // add new product to list
print_r($products);
Session::put('cart.items', $products);

}


New UPdate of code as per answer:



$cart1 = array("product_id" => $request->product_id,
"addtocart_totalmrp" => $request->addtocart_totalmrp,
"addtocart_totalsellingprice" => $request->addtocart_totalsellingprice,
"addtocart_auid" => $request->addtocart_auid);



if ($request->session()->has('cart')) {




Session::push('cart.items',$cart1); //putting that data to cart
$a=Session::get('cart', array());
//print_r(count($products));
dd($a);

} else {
echo 'session haas no data <pre>';
$products = [ $cart1 ]; // create a new cart and add a product in it
Session::put('cart.items', $products);
print_r($products);
}


code work perfectly but the issue of replacing started again something missing in my code?



here is what i want my code is this a sper asked in answers:



public function addtocart(Request $request) {


// if user logged in check login and add data directly to cart without session
if (Auth::check()) {
$user = Auth::user();
echo 'auth sucess';
// adding data to table directly //
} else {

// adding data to cart with session

$cart1 = array("product_id" => $request->product_id,
"addtocart_totalmrp" => $request->addtocart_totalmrp,
"addtocart_totalsellingprice" => $request->addtocart_totalsellingprice,
"addtocart_auid" => $request->addtocart_auid);
print_r('am current cart data : <pre>');
print_r($cart1);

// checking for session has cart or not

if (Session::has('cartdata')) {

echo ' session cart has data ';
} else {

echo 'session has no data add first data to session ';
}
}
}

}


I have tried a new concept of JSON structure can u plz help me where am wrong whenever I try to put data on to array it goes and stop pushing array after 2nd position
here is my Code:



// adding data to cart with session
$cartdata = array("product_id" => $request->product_id,
"addtocart_totalmrp" => $request->addtocart_totalmrp,
"addtocart_totalsellingprice" => $request->addtocart_totalsellingprice,
"addtocart_auid" => $request->addtocart_auid);
// print_r('am current cart data : <pre>');
//print_r($cartdata);


// checking for session has cart or not

if (Session::has('cartdata')) {

// dd(Session::get('cartdata'));

echo ' session cart has new data and it is :<pre> ';

//// fethcing old data from session
$productsfromsession = Session::get('cartdata'); // get existed array value


// decoding old data to normal array
$data=json_decode($productsfromsession);


//dd($data);


// pushing new data to old product array

array_push($data->products,$cartdata);


// saving new data and encoding it again
$newdata=json_encode($data);

dd($newdata); ///every time i add data to this array its stop after 2nd inserted
// removing old data from session
$request->session()->forget('cartdata');


// putting new data to session
Session::put('cartdata',$newdata);

// printing result
dd(Session::get('cartdata'));

//// printng current cart data after pusing new data


} else {


echo 'session has no data adding first data to session is <pre>';

// // putting first item to array
$products=$cartdata;
$cartdata=array("total_price"=>10,"products"=>$products);
$cartdata=json_encode($cartdata);
Session::put('cartdata', $cartdata);

$data = Session::all();
print_r($data);
}









share|improve this question















I am trying to make a cart and have the following requirements :
1.when user click adds to cart product added to the session like this:



    Array
(
[items] => Array
(
[0] => Array
(
[product_id] => 15
[addtocart_totalmrp] => 8953
[addtocart_totalsellingprice] => 7876
[addtocart_auid] => 7065bec17a101cf2,3025bec186520de3,6725bec18d7889a7
)

[1] => Array
(
[product_id] => 39
[addtocart_totalmrp] => 12654
[addtocart_totalsellingprice] => 11876
[addtocart_auid] => 1675bed115132bc6,755bed11033fe7d
)

),
[2] => Array////which is not coming after 2 it stops and start overwriting the 2nd position
(
[product_id] => 39
[addtocart_totalmrp] => 12654
[addtocart_totalsellingprice] => 11876
[addtocart_auid] => 1675bed115132bc6,755bed11033fe7d
)

)

)


here is my code:



$cart1 = array("product_id" => $request->product_id,
"addtocart_totalmrp" => $request->addtocart_totalmrp,
"addtocart_totalsellingprice" => $request->addtocart_totalsellingprice,
"addtocart_auid" => $request->addtocart_auid);


if ($request->session()->has('cart')) {

$products = Session::get('cart.items', array()); // get existed products or empty array

$products["items"] = $cart1; // add new product to list
echo'<pre>';
print_r($products);


Session::push('cart.items', $products); //putting that data to cart

print_r(count($products));
dd($products);

} else {
echo 'session haas no data';
$products["items"] = $cart1; // add new product to list
print_r($products);
Session::put('cart.items', $products);

}


my problem:
when I try to add a third product it is overwriting the 2nd one don't know how to do it.



I have tried this code and its working but there is item key I used once coming again any suggestion for this here is the array structure:



array:1 [
"items" => array:4 [
"items" => array:1 [
0 => array:4 [
"product_id" => "39"
"addtocart_totalmrp" => "12654"
"addtocart_totalsellingprice" => "11876"
"addtocart_auid" => "1675bed115132bc6,755bed11033fe7d"
]
]
0 => array:4 [
"product_id" => "28"
"addtocart_totalmrp" => "17145"
"addtocart_totalsellingprice" => "12165"
"addtocart_auid" => "5205bed102b6d856,8255bed103e3ce14"
]
1 => array:4 [
"product_id" => "15"
"addtocart_totalmrp" => "8953"
"addtocart_totalsellingprice" => "7876"
"addtocart_auid" => "7065bec17a101cf2,3025bec186520de3,6725bec18d7889a7"
]
2 => array:4 [
"product_id" => "15"
"addtocart_totalmrp" => "8953"
"addtocart_totalsellingprice" => "7876"
"addtocart_auid" => "7065bec17a101cf2,3025bec186520de3,6725bec18d7889a7"
]
]
]


my new code



if ($request->session()->has('cart')) {

// $productsfromsession = Session::get('cart', array()); // get existed products or empty array
// dd($productsfromsession);
// $products["items"] = $cart1; // add new product to list
// echo'<pre>';
// print_r($products);



Session::push('cart.items',$cart1); //putting that data to cart
$a=Session::get('cart', array());
//print_r(count($products));
dd($a);

} else {
echo 'session haas no data <pre>';
$products["items"] = $cart1; // add new product to list
print_r($products);
Session::put('cart.items', $products);

}


New UPdate of code as per answer:



$cart1 = array("product_id" => $request->product_id,
"addtocart_totalmrp" => $request->addtocart_totalmrp,
"addtocart_totalsellingprice" => $request->addtocart_totalsellingprice,
"addtocart_auid" => $request->addtocart_auid);



if ($request->session()->has('cart')) {




Session::push('cart.items',$cart1); //putting that data to cart
$a=Session::get('cart', array());
//print_r(count($products));
dd($a);

} else {
echo 'session haas no data <pre>';
$products = [ $cart1 ]; // create a new cart and add a product in it
Session::put('cart.items', $products);
print_r($products);
}


code work perfectly but the issue of replacing started again something missing in my code?



here is what i want my code is this a sper asked in answers:



public function addtocart(Request $request) {


// if user logged in check login and add data directly to cart without session
if (Auth::check()) {
$user = Auth::user();
echo 'auth sucess';
// adding data to table directly //
} else {

// adding data to cart with session

$cart1 = array("product_id" => $request->product_id,
"addtocart_totalmrp" => $request->addtocart_totalmrp,
"addtocart_totalsellingprice" => $request->addtocart_totalsellingprice,
"addtocart_auid" => $request->addtocart_auid);
print_r('am current cart data : <pre>');
print_r($cart1);

// checking for session has cart or not

if (Session::has('cartdata')) {

echo ' session cart has data ';
} else {

echo 'session has no data add first data to session ';
}
}
}

}


I have tried a new concept of JSON structure can u plz help me where am wrong whenever I try to put data on to array it goes and stop pushing array after 2nd position
here is my Code:



// adding data to cart with session
$cartdata = array("product_id" => $request->product_id,
"addtocart_totalmrp" => $request->addtocart_totalmrp,
"addtocart_totalsellingprice" => $request->addtocart_totalsellingprice,
"addtocart_auid" => $request->addtocart_auid);
// print_r('am current cart data : <pre>');
//print_r($cartdata);


// checking for session has cart or not

if (Session::has('cartdata')) {

// dd(Session::get('cartdata'));

echo ' session cart has new data and it is :<pre> ';

//// fethcing old data from session
$productsfromsession = Session::get('cartdata'); // get existed array value


// decoding old data to normal array
$data=json_decode($productsfromsession);


//dd($data);


// pushing new data to old product array

array_push($data->products,$cartdata);


// saving new data and encoding it again
$newdata=json_encode($data);

dd($newdata); ///every time i add data to this array its stop after 2nd inserted
// removing old data from session
$request->session()->forget('cartdata');


// putting new data to session
Session::put('cartdata',$newdata);

// printing result
dd(Session::get('cartdata'));

//// printng current cart data after pusing new data


} else {


echo 'session has no data adding first data to session is <pre>';

// // putting first item to array
$products=$cartdata;
$cartdata=array("total_price"=>10,"products"=>$products);
$cartdata=json_encode($cartdata);
Session::put('cartdata', $cartdata);

$data = Session::all();
print_r($data);
}






arrays laravel session cart






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 12:25

























asked Nov 22 at 6:18









Jagdish Sharma

17613




17613








  • 1




    use Session::put('cart.items',$products["items"])
    – Leena Patel
    Nov 22 at 6:27










  • thanks for your answer @leena Patel i will check it within a minute
    – Jagdish Sharma
    Nov 22 at 6:28












  • still not working for the third item it is still replacing same. any suggestion?
    – Jagdish Sharma
    Nov 22 at 6:33










  • The values are same may be that is why.
    – Leena Patel
    Nov 22 at 6:37














  • 1




    use Session::put('cart.items',$products["items"])
    – Leena Patel
    Nov 22 at 6:27










  • thanks for your answer @leena Patel i will check it within a minute
    – Jagdish Sharma
    Nov 22 at 6:28












  • still not working for the third item it is still replacing same. any suggestion?
    – Jagdish Sharma
    Nov 22 at 6:33










  • The values are same may be that is why.
    – Leena Patel
    Nov 22 at 6:37








1




1




use Session::put('cart.items',$products["items"])
– Leena Patel
Nov 22 at 6:27




use Session::put('cart.items',$products["items"])
– Leena Patel
Nov 22 at 6:27












thanks for your answer @leena Patel i will check it within a minute
– Jagdish Sharma
Nov 22 at 6:28






thanks for your answer @leena Patel i will check it within a minute
– Jagdish Sharma
Nov 22 at 6:28














still not working for the third item it is still replacing same. any suggestion?
– Jagdish Sharma
Nov 22 at 6:33




still not working for the third item it is still replacing same. any suggestion?
– Jagdish Sharma
Nov 22 at 6:33












The values are same may be that is why.
– Leena Patel
Nov 22 at 6:37




The values are same may be that is why.
– Leena Patel
Nov 22 at 6:37












2 Answers
2






active

oldest

votes

















up vote
1
down vote













Try this:



Session::push('cart.items', $cart1);


for more info on Session have a look at this:



https://laravel.com/docs/5.7/session#storing-data






share|improve this answer





















  • Thanks for reply JOSH i have tried push but still issue is same updating my new code wait..
    – Jagdish Sharma
    Nov 22 at 6:40


















up vote
1
down vote













In your new code, I find this a curious thing to do when there is no "cart" in the session.



$products["items"] = $cart1;  // add new product to 
print_r($products);
Session::put('cart.items', $products);


This above code will create an array of products nested under "items" which itself is nested.



[
"cart" => [
"items" => [
"items" => [ $cart1 ]
]
]
]


Session::push is fairly robust. It already pre-checks the value for the given key in the session before appending the given value to it. When the value isn't an array, it initializes an empty one and appends the given value.



The if-else branch becomes redundant when using Session::push.



$cart = [/*...*/];
Session::push('cart.items', $cart);





share|improve this answer























  • thanks for your reply Oluwafemi sule its is working fine now but the problem of replacing started again
    – Jagdish Sharma
    Nov 22 at 7:28










  • Oh I see. I think something is going on with the if-else and I'm unable to place that. Session::push already seems to be doing what the if-else does and makes the explicit check redundant.
    – Oluwafemi Sule
    Nov 22 at 8:56











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424915%2fhow-to-put-data-to-laravel-session-uing-array-of-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













Try this:



Session::push('cart.items', $cart1);


for more info on Session have a look at this:



https://laravel.com/docs/5.7/session#storing-data






share|improve this answer





















  • Thanks for reply JOSH i have tried push but still issue is same updating my new code wait..
    – Jagdish Sharma
    Nov 22 at 6:40















up vote
1
down vote













Try this:



Session::push('cart.items', $cart1);


for more info on Session have a look at this:



https://laravel.com/docs/5.7/session#storing-data






share|improve this answer





















  • Thanks for reply JOSH i have tried push but still issue is same updating my new code wait..
    – Jagdish Sharma
    Nov 22 at 6:40













up vote
1
down vote










up vote
1
down vote









Try this:



Session::push('cart.items', $cart1);


for more info on Session have a look at this:



https://laravel.com/docs/5.7/session#storing-data






share|improve this answer












Try this:



Session::push('cart.items', $cart1);


for more info on Session have a look at this:



https://laravel.com/docs/5.7/session#storing-data







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 at 6:36









Josh

60811




60811












  • Thanks for reply JOSH i have tried push but still issue is same updating my new code wait..
    – Jagdish Sharma
    Nov 22 at 6:40


















  • Thanks for reply JOSH i have tried push but still issue is same updating my new code wait..
    – Jagdish Sharma
    Nov 22 at 6:40
















Thanks for reply JOSH i have tried push but still issue is same updating my new code wait..
– Jagdish Sharma
Nov 22 at 6:40




Thanks for reply JOSH i have tried push but still issue is same updating my new code wait..
– Jagdish Sharma
Nov 22 at 6:40












up vote
1
down vote













In your new code, I find this a curious thing to do when there is no "cart" in the session.



$products["items"] = $cart1;  // add new product to 
print_r($products);
Session::put('cart.items', $products);


This above code will create an array of products nested under "items" which itself is nested.



[
"cart" => [
"items" => [
"items" => [ $cart1 ]
]
]
]


Session::push is fairly robust. It already pre-checks the value for the given key in the session before appending the given value to it. When the value isn't an array, it initializes an empty one and appends the given value.



The if-else branch becomes redundant when using Session::push.



$cart = [/*...*/];
Session::push('cart.items', $cart);





share|improve this answer























  • thanks for your reply Oluwafemi sule its is working fine now but the problem of replacing started again
    – Jagdish Sharma
    Nov 22 at 7:28










  • Oh I see. I think something is going on with the if-else and I'm unable to place that. Session::push already seems to be doing what the if-else does and makes the explicit check redundant.
    – Oluwafemi Sule
    Nov 22 at 8:56















up vote
1
down vote













In your new code, I find this a curious thing to do when there is no "cart" in the session.



$products["items"] = $cart1;  // add new product to 
print_r($products);
Session::put('cart.items', $products);


This above code will create an array of products nested under "items" which itself is nested.



[
"cart" => [
"items" => [
"items" => [ $cart1 ]
]
]
]


Session::push is fairly robust. It already pre-checks the value for the given key in the session before appending the given value to it. When the value isn't an array, it initializes an empty one and appends the given value.



The if-else branch becomes redundant when using Session::push.



$cart = [/*...*/];
Session::push('cart.items', $cart);





share|improve this answer























  • thanks for your reply Oluwafemi sule its is working fine now but the problem of replacing started again
    – Jagdish Sharma
    Nov 22 at 7:28










  • Oh I see. I think something is going on with the if-else and I'm unable to place that. Session::push already seems to be doing what the if-else does and makes the explicit check redundant.
    – Oluwafemi Sule
    Nov 22 at 8:56













up vote
1
down vote










up vote
1
down vote









In your new code, I find this a curious thing to do when there is no "cart" in the session.



$products["items"] = $cart1;  // add new product to 
print_r($products);
Session::put('cart.items', $products);


This above code will create an array of products nested under "items" which itself is nested.



[
"cart" => [
"items" => [
"items" => [ $cart1 ]
]
]
]


Session::push is fairly robust. It already pre-checks the value for the given key in the session before appending the given value to it. When the value isn't an array, it initializes an empty one and appends the given value.



The if-else branch becomes redundant when using Session::push.



$cart = [/*...*/];
Session::push('cart.items', $cart);





share|improve this answer














In your new code, I find this a curious thing to do when there is no "cart" in the session.



$products["items"] = $cart1;  // add new product to 
print_r($products);
Session::put('cart.items', $products);


This above code will create an array of products nested under "items" which itself is nested.



[
"cart" => [
"items" => [
"items" => [ $cart1 ]
]
]
]


Session::push is fairly robust. It already pre-checks the value for the given key in the session before appending the given value to it. When the value isn't an array, it initializes an empty one and appends the given value.



The if-else branch becomes redundant when using Session::push.



$cart = [/*...*/];
Session::push('cart.items', $cart);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 at 21:13

























answered Nov 22 at 7:16









Oluwafemi Sule

10.5k1330




10.5k1330












  • thanks for your reply Oluwafemi sule its is working fine now but the problem of replacing started again
    – Jagdish Sharma
    Nov 22 at 7:28










  • Oh I see. I think something is going on with the if-else and I'm unable to place that. Session::push already seems to be doing what the if-else does and makes the explicit check redundant.
    – Oluwafemi Sule
    Nov 22 at 8:56


















  • thanks for your reply Oluwafemi sule its is working fine now but the problem of replacing started again
    – Jagdish Sharma
    Nov 22 at 7:28










  • Oh I see. I think something is going on with the if-else and I'm unable to place that. Session::push already seems to be doing what the if-else does and makes the explicit check redundant.
    – Oluwafemi Sule
    Nov 22 at 8:56
















thanks for your reply Oluwafemi sule its is working fine now but the problem of replacing started again
– Jagdish Sharma
Nov 22 at 7:28




thanks for your reply Oluwafemi sule its is working fine now but the problem of replacing started again
– Jagdish Sharma
Nov 22 at 7:28












Oh I see. I think something is going on with the if-else and I'm unable to place that. Session::push already seems to be doing what the if-else does and makes the explicit check redundant.
– Oluwafemi Sule
Nov 22 at 8:56




Oh I see. I think something is going on with the if-else and I'm unable to place that. Session::push already seems to be doing what the if-else does and makes the explicit check redundant.
– Oluwafemi Sule
Nov 22 at 8:56


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424915%2fhow-to-put-data-to-laravel-session-uing-array-of-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks