I'm trying to do this:
If the user navigates to the cart page (CartController
), a $cart
array gets generated from data from DB (the cart is stored in DB). But some changes are made to the data, such as replacing price with the user's custom price.
If the user clicks Finish order, on the bottom of the cart page, he will submit a form to OrdersController@store
, which would need to generate the same array CartController
generates.
To avoid this expensive operation, I want to store the $cart
array in session, but only for one more request. When the user clicks on Finish order and submits the form to OrdersController@store, the array won't have to be generated again. If he navigates away from the cart page, the array will no longer be held in session storage.
However, I haven't been able to use flash() or reflash() successfully.
I tried doing reflash() in both controllers but that didn't seem to make a difference. I must have used it incorrectly. I tried $request->session()->reflash();dd($request)
in store as well as request()->session()->flash('cart', $cart);$request->session()->reflash()
in CartController.
So I'm trying to hold the data in flash for the next page request.
Code of CartController
and OrdersController@store
: https://gist.github.com/Shifterovich/e4cf7e23421d495a8cd9a358dd4849d0
Relevant experiments with flash():
Laravel's session()->flash('foo', 'bar')
stores 'foo' => 'bar'
for the next request - if you flash something in controller, the view will see it.
If I do
session()->flash('error', 'test');
return view('account');
I get:
"_flash" => array:2 [▼
"old" => []
"new" => array:1 [▼
0 => "error"
]
]
"login_web_xxxxx" => 1
"error" => "test"
It's in the new
array. If I do:
And submit the form
public function changeInfo() {
// some code
session()->flash('success', 'Changed');
return redirect()->back();
}
I get:
"_flash" => array:2 [▼
"old" => array:1 [▼
0 => "success"
]
"new" => []
]
"login_web_xxxxx" => 1
"success" => "Changed"
It's in the old
array.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire