lundi 23 mai 2016

Laravel 5.1 - Store input data in my session

I have a view show.php of my single product, when user click on ADD PRODUCT my controller CartController.php update my session cart and update my cartSession table, Now i'm tryng to put new data (Color input,Size input, Quantity input) on my session and also in my CartSession table. But i dont know why it doesnt work.

-I think that the principal problem is that $request->get('inputs') doesnt pass to my CartController.php , i tryed to return $request->all() and there is not nothing.

CartController.php

namespace dixard\Http\Controllers;

use Illuminate\Http\Request;

use dixard\Http\Requests;
use dixard\Http\Controllers\Controller;

use dixard\Product;
use dixard\CartSession;
use dixard\CartItem;
use dixard\Shipping;
use Session; 
// i think that all classes are ok

public function add(Product $product, CartSession $CartSession,Request $request)
{
    $id = $request->get('id');
    $cart  = \Session::get('cart'); 

    $product->quantity = $request->get('qty');
    $product->size = $request->get('size');
    $product->color = $request->get('color');
    $cart[$product->id] = $product; 

    \Session::put('cart', $cart); 

    return $request->all(); // here i tryed to get all inputs but it did show me nothing result
    $subtotal = 0;

    foreach($cart as $producto){
        $subtotal += $producto->quantity * $producto->price;
    }   

    $session_code = Session::getId();
    $CartSession = new CartSession();

    $session_exist = CartSession::where('session_code', $session_code)->orderBy('id', 'desc')->first();

    if (isset($session_exist)) {


            $s = new CartSession;

            $data = array(

            'subtotal' => $subtotal,

            );

            $s->where('session_code', '=', $session_code)->update($data);


    }else {

            $CartSession = new CartSession();
            $CartSession->session_code = $session_code; 
            $CartSession->subtotal = $subtotal; 
            $CartSession->save(); 
    }

    //return $cart;
    //salveremo tutte le informazioni nel array cart nella posizione slug

    foreach($cart as $producto){
        $this->saveCartItem($producto, $CartSession->id, $session_code, $cart);
    }

    return redirect()->route('cart-show');


}

Routes.php

Route::bind('product', function($id) {
     return dixard\Product::where('id', $id)->first();
    });

     Route::get('cart/add/{product}', [

            'as' => 'cart-add',
            'uses' => 'CartController@add'

            ]);

show.php view

  • Get Data about the product is ok, title, description, color avaible, price ecc. all information pass to my view.
{!! Form::open(['route'=> ['cart-add', $product->id],'class'=>'form-horizontal form-label-left'])!!}

                <input type="hidden" name="_method" value="PUT">
                    <input type="hidden" name="id" value="">

                        <div class="row">
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="p_color">Colore</label>

                                    <select name="color" id="p_size" class="form-control">
                                        @foreach($colors as $color)


                                    <option value=""></option>

                                        @endforeach

                                    </select>

                                </div>
                            </div>

                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="size">Size</label>
                                    <select name="size" id="p_size" class="form-control">
                                        <option value="XS">XS</option>
                                        <option value="S">S</option>
                                        <option value="M">M</option>
                                        <option value="L">L</option>
                                        <option value="XL">XL</option>
                                    </select>
                                </div>
                            </div>

                            <div class="col-md-3">
                                <div class="form-group">
                                    <label for="qty">Quantity</label>
                                    <select name="qty" id="p_qty" class="form-control">
                                        <option value="">1</option>
                                        <option value="">2</option>
                                        <option value="">3</option>
                                    </select>
                                </div>
                            </div>
                        </div>


                    <div class="product-list-actions">
                        <span class="product-price">
                            <span class="amount"></span>



                     <input  type="submit" class="btn btn-lg btn-primary" >
                       ADD PRODUCT
                        </input>
                    </div><!-- /.product-list-actions -->
                    {!! Form::close() !!}

Thank you for your help!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire