F
F
Flasher2016-07-01 10:38:18
Laravel
Flasher, 2016-07-01 10:38:18

When adding a product to the cart, I add all the data to the session. What if I add another item?

When adding a product to the cart, I add all the data to the session. What if I add another item? I can’t figure out how to process, I have something like this output in the view:

@if(Session::has('user_order_product'))
    {{ Session::get('user_order_product')->title }}<br>
    {{ Session::get('user_order_color') }}<br>
    {{ Session::get('user_order_size') }}<br>
    {{ Session::get('user_order_count') }}<br>
@endif

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
IceJOKER, 2016-07-01
@IceJOKER

You somehow incorrectly store data in the session, why save each value separately when you can save one product as an array.

$item = ['id' => 1, 'title' => 'someTitle', 'other_field' => 'otherFieldlValue'];
$_SESSION['cart_items'][] = $item; 
//либо так
$_SESSION['cart_items'][$item['id']] = $item;
//второй способ предотвращает повторное добавление одного и того же товара

And for converting under Laravel, read the documentation - https://laravel.com/docs/5.2/session

Влад, 2016-07-01
@Result007

Привет! Вот серия уроков про Интернет-магазин на Laravel 5. Может возьмешь что-нибудь полезное :) Там через JS корзина на сколько я знаю.
otdel-devsite.ru/laravel-5-%D0%BF%D0%B8%D1%88%D0%B...

Александр Фанамуров, 2016-07-07
@Fanamura

Не мучайтесь и попробуйте использовать gloudemans/shoppingcart
https://github.com/Crinsane/LaravelShoppingcart

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question