Laravel 5.1 - 在我的会话中存储输入数据

Laravel 5.1 - Store input data in my session

我有一个视图 show.php 我的单一产品,当用户点击添加产品时,我的控制器 CartController.php 更新我的会话购物车并更新我的 cartSession table, 现在我正在尝试将新数据(颜色输入、尺寸输入、数量输入)放入我的会话以及我的 CartSession table。但我不知道为什么它不起作用。

-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 查看

  • 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() !!}

感谢您的帮助!

您必须明确地将 post 方法设置为 "get",或者您需要更改您的路由器以接受请求 post。即使您按名称调用路由,Form::open 默认为 "POST"

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

选项 1.更改

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

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

选项 2. 更改

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

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