PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 11, 2022

[FIXED] Searching items in cart using laravel crinsane cart

 March 11, 2022     laravel, laravel-5, php, shopping-cart     No comments   

Issue

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)?


Solution

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.



Answered By - Ohgodwhy
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing