1
votes

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 doesn't work.

-I think that the principal problem is that $request->get('input') doesn't pass to my CartController.php , i tried 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 tried to get all inputs but it shows 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="{{$product->id}}">

                        <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="{{ $color->color }}">{{ $color->color }}</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">{{$product->price}}</span>



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

Thank you for your help!

1

1 Answers

2
votes

You have to explicitly set the post-method to be "get" or you need to change your router to accept the request as post. Even though you're calling the route by name, Form::open defaults to "POST"

https://laravelcollective.com/docs/5.2/html#opening-a-form

Option 1. Change

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

to

Route::post('cart/add/{product}',...

Option 2. Change

{!! Form::open(['route'=> ['cart-add', $product->id],'class'=>'form-horizontal form-label-left'])!!}

to

{!! Form::open(['method'=>'GET', 'route'=> ['cart-add', $product->id],'class'=>'form-horizontal form-label-left'])!!}