1
votes

I'm making a cart. For now I can add products to session and view them in my cart. This is my CartController functions:

Add function:

public function add(Request $request, $id)
{
    $request->session()->push('cart', $id);
    flash()->success('Prekė sėkmingai pridėtą į krepšelį.');
    return redirect('/prekes');
}

Show function:

public function show()
{

    $products = session('cart');
    $products = Product::find($products);
    return view('cart.show', compact('products'));
}

And this is a Product page:

    <div class="mainContainer">
        <h1>{{$product->title}} <i class="fa fa-shopping-cart" aria-hidden="true"></i></h1><br>
                <img style="height: 200px" src="{{$product->image}}"><br>
                <p>{!! $product->description !!}</p>
        <div class="bootstrap-wrapper">
            <a style="float: right; margin-top: -150px; margin-right: 300px; font-size: 24px"
               href="/prideti-i-krepseli/{{$product->id}}" class="btn btn-success">
                <i class="glyphicon glyphicon-shopping-cart"></i> Pridėti į krepšelį</a><br>
            {{number_format($product->price, 2, '.', ',')}} EUR
        </div>
    </div>

So if I want to send some values from a form, how would I insert them in session ? Should I write post method code in add function ? Can you give me some example ? I want to make a form that will set a quantity for each item.

1
@CarlosCarucce Okay, so I should just set empty field called "quantity" and then send it to session in my Controller ?labas GamePage visogero
@CarlosCarucce Still how to add new value to my controller ?labas GamePage visogero
Hey man a simple google search will get you useful results laravel.com/docs/5.1/sessionpeterchaula

1 Answers

1
votes

From what i understand you want to add array of values into session (shopping cart can be represented as array).

To store values in array and not as single session variable you need to change your

public function add(Request $request, $id)
{
    $request->session()->push('cart', $id);
    flash()->success('Prekė sėkmingai pridėtą į krepšelį.');
    return redirect('/prekes');
}

with

public function add(Request $request, $id, $quantity)
{
    $product_from_db = Product::find($id);
    $product = [];
    $product['id'] = $id;
    //you can add all data you need like this etc...
    $product['name'] = $product_from_db->name;
    $product['quantity'] = $quantity;        
    $request->session()->push('cart', array_merge((array)Session::get('cart',[]), $product));    
    flash()->success('Prekė sėkmingai pridėtą į krepšelį.');
    return redirect('/prekes');
}

To receive data from session replace

public function show()
{    
    $products = session('cart');
    $products = Product::find($products);
    return view('cart.show', compact('products'));
}

with

public function show()
{    
    $products = session('cart');
    return view('cart.show', compact('products'));
}

And in your View you show that data like this

@if(Session::has('cart'))
    @foreach(Session::get('cart') as $item)
        {{$item['id']}}
        {{$item['name']}}
        {{$item['quantity']}}
    @endforeach
@endif