<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                還是拿來主義編程思想,既然別人實現了,就不再造輪子了。 `LaravelShoppingcart`是Laravel下的一個購物車實現,項目地址: > https://github.com/Crinsane/LaravelShoppingcart 文檔翻譯: 針對Laravel 4的簡單購物車實現。 ## 安裝 通過composer安裝,編輯 `composer.json` 文件,如下: #### Laravel4.2及以下 ~~~ "require": { "laravel/framework": "4.2.*", "gloudemans/shoppingcart": "~1.2" } ~~~ #### Laravel5 ~~~ "require": { "laravel/framework": "5.0.*", "gloudemans/shoppingcart": "dev-master" } ~~~ #### 接著運行更新命令 ~~~ composer update ~~~ 添加以下代碼到 `app/config/app.php` 的 `service providers` 數組里 ~~~ 'Gloudemans\Shoppingcart\ShoppingcartServiceProvider' ~~~ 添加以下代碼到`aliases`數組中 ~~~ 'Cart' => 'Gloudemans\Shoppingcart\Facades\Cart', ~~~ 完成以上工作就可以在你的項目中使用了。 ## 使用 Shoppingcart提供了以下的可用方法: #### Cart:add() ~~~ /** * Add a row to the cart * 向購物車添加新行 * * @param string|Array $id Unique ID of the item|Item formated as array|Array of items * -參數 字符串|數組 $id item的唯一id | Item格式化成數組 | items 數組 * @param string $name Name of the item * -參數 字符串 $name item 名稱 * @param int $qty Item qty to add to the cart * -param int $qty 添加到購物車的item的數量 * @param float $price Price of one item * -param float $price item 價格 * @param Array $options Array of additional options, such as 'size' or 'color' * @param Array $options 附加參數數組, 諸如'size' 或 'color' */ // Basic form 基礎表單 Cart::add('293ad', 'Product 1', 1, 9.99, array('size' => 'large')); // Array form 數組表單 Cart::add(array('id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 9.99, 'options' => array('size' => 'large'))); // Batch method 批量方法 Cart::add(array( array('id' => '293ad', 'name' => 'Product 1', 'qty' => 1, 'price' => 10.00), array('id' => '4832k', 'name' => 'Product 2', 'qty' => 1, 'price' => 10.00, 'options' => array('size' => 'large')) )); ~~~ #### Cart:update() ~~~ /** * Update the quantity of one row of the cart * * @param string $rowId The rowid of the item you want to update * @param integer|Array $attribute New quantity of the item|Array of attributes to update * @return boolean */ $rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709'; Cart::update($rowId, 2); OR Cart::update($rowId, array('name' => 'Product 1')); ~~~ #### Cart::remove() ~~~ /** * Remove a row from the cart * * @param string $rowId The rowid of the item * @return boolean */ $rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709'; Cart::remove($rowId); ~~~ #### Cart::get() ~~~ /** * Get a row of the cart by its ID * * @param string $rowId The ID of the row to fetch * @return CartRowCollection */ $rowId = 'da39a3ee5e6b4b0d3255bfef95601890afd80709'; Cart::get($rowId); ~~~ #### Cart::content() ~~~ /** * Get the cart content * * @return CartCollection */ Cart::content(); ~~~ #### Cart::destroy() ~~~ /** * Empty the cart 清空購物車 * * @return boolean */ Cart::destroy(); ~~~ #### Cart:total() ~~~ /** * Get the price total 獲取總價格 * * @return float */ Cart::total(); ~~~ #### Cart:count() ~~~ /** * Get the number of items in the cart * * @param boolean $totalItems Get all the items (when false, will return the number of rows) * @return int */ Cart::count(); // Total items Cart::count(false); // Total rows ~~~ #### Cart::search() ~~~ /** * Search if the cart has a item * * @param Array $search An array with the item ID and optional options * @return Array|boolean */ Cart::search(array('id' => 1, 'options' => array('size' => 'L'))); // Returns an array of rowid(s) of found item(s) or false on failure ~~~ #### Collections 顯而易見,`Cart::content()`和`Cart::get()`返回一個集合,`CartCollection`和`CartRowCollection`。 這些集合擴展了原始的`Laravel 4`的`collection`類,所以這個類的所有方法同樣的可以被使用在購物車類中。 ## 實例 現在擴展包可以支持多個購物車實例,工作方式像這樣: 你可以使用`Cart::instance(‘newInstance’)`設置當前的購物車實例,此刻,有效的購物車實例是`newInstance`,所以你添加、移除或獲取購物車內容,操作的都是`newInstance`。如果你需要選擇其它實例,你只需要調用`Cart::instance(‘otherInstance’)` ~~~ Cart::instance('shopping')->add('192ao12', 'Product 1', 1, 9.99); // Get the content of the 'shopping' cart Cart::content(); Cart::instance('wishlist')->add('sdjk922', 'Product 2', 1, 19.95, array('size' => 'medium')); // Get the content of the 'wishlist' cart Cart::content(); // If you want to get the content of the 'shopping' cart again... Cart::instance('shopping')->content(); // And the count of the 'wishlist' cart again Cart::instance('wishlist')->count(); ~~~ > 備注1:如果你不重新設置,購物車永遠是最后一個實例 > 備注2:默認的購物車實例名稱是mian,所以Cart::content();等同于Cart::instance(‘main’)->content()。 ## Models 一個新特性是可以使為購物車的item關聯一個模型。假設在你的應用中有一個product模型,通過新的`associate()`方法,可以通知購物車里的項關聯到product模型。 這樣你可以從 `CartRowCollection` 訪問你的模型。 下面是一個示例: ~~~ <?php /** * Let say we have a Product model that has a name and description. */ Cart::associate('Product')->add('293ad', 'Product 1', 1, 9.99, array('size' => 'large')); $content = Cart::content(); foreach($content as $row) { echo 'You have ' . $row->qty . ' items of ' . $row->product->name . ' with description: "' . $row->product->description . '" in your cart.'; } ~~~ 訪問模型的主鍵和關聯模型的名稱相同。associate()還有第二個參數,是可選的,用于指定模型的命名空間。 ## 異常錯誤 當發生錯誤時,購物車模塊將拋出異常。使用購物車模塊可以很容易的調試錯誤,或者基于異常的類型處理錯誤。購物車模塊可以拋出以下異常: |Exception|Reason| |---------|------| |ShoppingcartInstanceException|When no instance is passed to the instance() method 沒有實例傳遞給instance()方法| |ShoppingcartInvalidItemException|When a new product misses one of it’s arguments (id,name, qty, price) 當新產品缺少一個參數| |ShoppingcartInvalidPriceException|When a non-numeric price is passed當非數值的價格被傳遞| |ShoppingcartInvalidQtyException|When a non-numeric quantity is passed當非數值的數量被傳遞| |ShoppingcartInvalidRowIDException|When the $rowId that got passed doesn’t exists in the current cart當當前的購物車并不存在被傳來的$roeid| |ShoppingcartUnknownModelException|When an unknown model is associated to a cart row當一個未知的模型被關聯到購物車的行| ## Events 下面是購物車監聽的事件: |Event|Fired| |-----|-----| |cart.add($item)|When a single item is added當單個item被添加| |cart.batch($items)|When a batch of items is added當一批item被添加| |cart.update($rowId)|When an item in the cart is updated購物車單個item跟更新| |cart.remove($rowId)|When an item is removed from the cart當一個item被從購物車中移除| |cart.destroy()|When the cart is destroyed當購物車被清空| #### 示例: 下面的示例展示了如果在table中呈現購物車的內容 ~~~ // Controller Cart::add('192ao12', 'Product 1', 1, 9.99); Cart::add('1239ad0', 'Product 2', 2, 5.95, array('size' => 'large')); // View <table> <thead> <tr> <th>Product</th> <th>Qty</th> <th>Item Price</th> <th>Subtotal</th> </tr> </thead> <tbody> <?php foreach($cart as $row) :?> <tr> <td> <p><strong><?php echo $row->name;?></strong></p> <p><?php echo ($row->options->has('size') ? $row->options->size : '');?></p> </td> <td><input type="text" value="<?php echo $row->qty;?>"></td> <td>$<?php echo $row->price;?></td> <td>$<?php echo $row->subtotal;?></td> </tr> <?php endforeach;?> </tbody> </table> ~~~ * * * * * 原文地址:[http://www.zhangxihai.cn/archives/189](http://www.zhangxihai.cn/archives/189)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看