【问题标题】:LaravelSession::insertUpdate() issues while using laravel moltin shopping cart使用 laravel moltin 购物车时的 LaravelSession::insertUpdate() 问题
【发布时间】:2014-08-22 16:56:33
【问题描述】:

我正在尝试在我的 Laravel 4 应用中使用 Moltin Cart

  • 我通过 composer 安装了 Moltin Cart,
  • 然后添加服务提供者 ('Moltin\Cart\CartServiceProvider')
  • 并添加了别名 ('Cart' => 'Moltin\Cart\Facade')。

现在我只想登录用户访问购物车,所以我添加了

$this->beforeFilter('auth', array('only' => 'postAddToCart', 'getCart', 'getRemoveItem'));

在我的 storeController 中。

但是当我尝试访问时出现此错误

Declaration of Moltin\Cart\Storage\LaravelSession::insertUpdate() must be compatible with Moltin\Cart\StorageInterface::insertUpdate(Moltin\Cart\Item\Line $item)

我的商店控制器是:

<?php

class StoresController extends \BaseController {

public function __construct()
{
    parent::__construct();
    $this->beforeFilter('csrf', array('on' => 'post'));
    $this->beforeFilter('auth', array('only' => 'postAddToCart', 'getCart', 'getRemoveItem'));
}

public function getIndex()
{
    return  View::make('stores.index')
                    ->with('products', Product::take(4)->orderBy('created_at', 'DESC')->get());
}

public function getView($id)
{
    return  View::make('stores.view')
                    ->with('product', Product::find($id));
}

public function getCategory($category_id)
{
    return  View::make('stores.category')
                    ->with('products', Product::where('category_id', '=', $category_id)->paginate(6))
                    ->with('category', Category::find($category_id));   
}

public function getSearch()
{
    $keyword = Input::get('keyword');
    return  View::make('stores.search')
                    ->with('products', Product::where('title', 'LIKE', '%'. $keyword . '%')->get())
                    ->with('keyword', $keyword);
}

public function postAddToCart()
{
    $product    = Product::find(Input::get('id'));
    $qunatity = Input::get('qunatity');

    Cart::insert(array(
        'id'                => $product->id,
        'name'          => $product->name,
        'price'         => $product->price,
        'qunatity'  => $qunatity,
        'image'         => $product->image
    ));

    return Redirect::to('store/cart');
}

public function getCart()
{
    return View::make('stores.cart')->with('products', Cart::contents());
}

public function getRemoveItem($identifier)
{
    $product = Cart::item($identifier);
    $product->remove();
    return Redirect::to('store/cart');
  }
}

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:

    简短的回答:

    假设 moltin/cartmoltin/laravel-cart 存储库已同步,继续安装。

    长答案:

    如错误消息所述:

    • Moltin\Cart\StorageInterface::insertUpdate(Moltin\Cart\Item\Line $item)
    • Moltin\Cart\Storage\LaravelSession::insertUpdate()

    没有相同的签名。

    它们应该是同步的!该问题与您的StoresController 完全无关。

    我检查过:

    我发现签名可以作为我发帖的时间。


    namespace Moltin\Cart;
    
    interface StorageInterface
    {
        /**
         * Add or update an item in the cart
         * 
         * @param  Item   $item The item to insert or update
         * @return void
         */
        public function insertUpdate(Item $item); // <<<< This is it!
    
        ...
    

    }


    namespace Moltin\Cart\Storage;
    
    use Moltin\Cart\Item;
    ...
    
    class LaravelSession implements \Moltin\Cart\StorageInterface
    {
        ...
    
        /**
         * Add or update an item in the cart
         * 
         * @param  Item   $item The item to insert or update
         * @return void
         */
        public function insertUpdate(Item $item)  // <<<< This is it!
        {
            ...
        }
    
        ...
    
    }
    

    我的猜测是,当存储库(知道 moltin/cartmoltin/laravel-cart 的依赖项)对于某些人不同步时,您进行了安装原因。

    【讨论】:

      猜你喜欢
      • 2016-04-16
      • 1970-01-01
      • 2020-11-12
      • 2011-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多