Adwin
Adwin

Reputation: 195

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='{{url("cart?id=$item->id&add=1")}}'> + </a>
        <input type="text" name="qty" value="{{$item->qty}}" autocomplete="off" size="2">
        <a href='{{url("cart?id=$item->id&minus=1")}}'> - </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 1 (banana)?

Upvotes: 2

Views: 3204

Answers (3)

Simon Angatia
Simon Angatia

Reputation: 860

TRY:

$cart $item = Cart::content()->where('rowId', $id)->first();

if you have several cart instances:

$cart_item = Cart::instance('instance')->content()->where('rowId', $id)->first();

If you want to search based on the cart item options:

$cart_item = Cart::content()->where('options', function($q){
$q->where('name', 'simon');
})->first();

Upvotes: 0

Ishaan
Ishaan

Reputation: 1340

May this also helps someone.

Try to find by where() function like -

Cart::content()->where('id', $id);

Where id is the item id you want to search for.

Find it here.

Upvotes: 1

Ohgodwhy
Ohgodwhy

Reputation: 50787

You can do it just like this:

Cart::search(function($cartItem, $rowId) {
    return $cartItem->id == 1;
});

The search method is a wrapper for Laravel's filter method on collections.

A Closure is simply a function passed to a function that may or may not receive variables as arguments from the parent function.

If you want to use the $request variable inside of this Closure you will have to use() it:

Cart::search(function($cartItem, rowId) use($request) {
    return $cartItem->id == $request->id;
});

Of course make sure that $request->has('id') before you attempt to access that property.

Upvotes: 1

Related Questions