dimanche 4 juin 2017

Searching items in cart using laravel crinsane cart

Am trying to get an item from a cart using Crinsane Larvel Shopping Cart. The documentation talks about using a closure. I do not understand the example provided.

I am using Laravel 5.3, am trying to search items from the shopping cart using information from the Request object. This is my code :

View

@foreach($cart as $item)
<td>
    <div>
        <a href=''> + </a>
        <input type="text" name="qty" value="" autocomplete="off" size="2">
        <a href=''> - </a>
    </div>
</td>
@endforeach

Route

Route::get('/cart', 'CartController@getCart');

Controller

public function getCart(){

    //increment the quantity
    if (Request::get('id') && (Request::get('add')) == 1) {

        $inputItem = Request::get('id');
        //$rowId = Cart::search(['id' => Request::get('id')]);
        $cartItem = Cart::get($rowId);
        Cart::update($rowId, $cartItem->qty+1);
    }

    //decrease the quantity
    if (Request::get('id') && (Request::get('minus')) == 1) {

        $inputItem = Request::get('id');
        //$rowId = Cart::search(['id' => Request::get('id')]);
        $cartItem = Cart::get($rowId);
        Cart::update($rowId, $cartItem->qty-1);
    }

}

Cart Content Collection

`Collection {#318 ▼
  #items: array:2 [▼
    "027c91341fd5cf4d2579b49c4b6a90da" => CartItem {#319 ▼
      +rowId: "027c91341fd5cf4d2579b49c4b6a90da"
      +id: "1"
      +qty: 1
      +name: "Banana"
      +price: 35.0
      +options: CartItemOptions {#320 ▼
        #items: []
      }
      -associatedModel: null
      -taxRate: 21
    }
    "370d08585360f5c568b18d1f2e4ca1df" => CartItem {#321 ▼
      +rowId: "370d08585360f5c568b18d1f2e4ca1df"
      +id: "2"
      +qty: 7
      +name: "Melon"
      +price: 64.0
      +options: CartItemOptions {#322 ▼
        #items: []
      }
      -associatedModel: null
      -taxRate: 21
    }
  ]
}`

How can I use Cart::search() to find the Cart item with id of 1 (banana)?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2s7cCvi
via IFTTT

Aucun commentaire:

Enregistrer un commentaire