Laravelshoppingcart: How to search/get a cart values/item with its id not rowId

Created on 31 Oct 2017  路  8Comments  路  Source: Crinsane/LaravelShoppingcart

public function addCart($id)
{
$products = MarketProduct::find($id);

    $cartproduct = Cart::search(array('id' => $id, 'options' => array('available_quantity' => ?)));

     return  $cart_available_quantity = $cartproduct->options->available_quantity; 

    $available_quantity = $products->product->available_quantity;

    then compare $cart_available_quantity and $available_quantity before adding more qty

    $result = Cart::add([
        'id' => $id,
        'name' => $products->title,
        'qty' => 1,
        'price' => $products->mrp(),
        'options' => [
            'image' => $products->thumbnail(),
            'available_quantity' => $available_quantity
            ]
        ]);

    return redirect()->back();


}

Most helpful comment

Cart::content()->where('id', $id); this code did the tricks for me...

All 8 comments

Use the search method

Sir, your answer is not clear to me. I already used the search method. Have close look on the code
$cartproduct = Cart::search(array('id' => $id, 'options' => array('available_quantity' => ?)));

basically I need this:
$products = App\MarketProduct::find($id);
$cartproducts = Cart::search('id' == $id);
then compare quantity from both array...
I hope I can make you understand.

Thanks

Cart::content()->where('id', $id); this code did the tricks for me...

@wasid you saved my day man very thanks

I also can't understand how the search function works here.
But the @wasid 's solution also works for me. Thanks man.馃憦

Cart::content()->where('id', $id); this code did the tricks for me...

Output :

Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[52301c99fa63b03bd5c487d5643a4b70] => Gloudemans\Shoppingcart\CartItem Object
(
[rowId] => 52301c99fa63b03bd5c487d5643a4b70
[id] => 15
[qty] => 14
[name] => a
[price] => 17
[options] => Gloudemans\Shoppingcart\CartItemOptions Object
(
[items:protected] => Array
(
[image] => images/product_image/150.jpg
)
)
[associatedModel:Gloudemans\Shoppingcart\CartItem:private] =>
[taxRate:Gloudemans\Shoppingcart\CartItem:private] => 0
)
)
)

@wasid But how to get the qty ?

Sorry if i misunderstand the topic but using the search method is just Collection::filter, you should be able to iterate the returning collection or use first() and then be able to access the quantity as it is a public property of the CartItem class like this: $item->qty
https://github.com/Crinsane/LaravelShoppingcart/blob/f460ab7312cce32cb428cf39a500a2d16600b1d6/src/CartItem.php#L30

So out of my head something like that should work:

$cart->search(function($value, $key){
  return $value->id === $id;
})->first()->qty;

@bumbummen99, Its very simple ..................

$asd = Cart::content()->where('id', $request->product_id);
$cart_indqty = 0;

foreach ($asd as $row) {
$cart_indqty = $row->qty;
}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pelachile picture pelachile  路  8Comments

gauravojha picture gauravojha  路  8Comments

sobhanattar picture sobhanattar  路  4Comments

yansentan picture yansentan  路  6Comments

abdullahkaamil picture abdullahkaamil  路  3Comments