使用 laravel crinsane 购物车搜索购物车中的商品
Searching items in cart using laravel crinsane cart
我正在尝试使用 Crinsane Larvel Shopping Cart 从购物车中获取商品。
该文档讨论了使用闭包。我不明白所提供的示例。
我正在使用 Laravel 5.3,我正在尝试使用 Request 对象中的信息从购物车中搜索商品。这是我的代码:
查看
@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::get('/cart', 'CartController@getCart');
控制器
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);
}
}
购物车内容合集
`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
}
]
}`
如何使用 Cart::search()
找到 ID 为 1 的购物车商品(香蕉)?
你可以这样做:
Cart::search(function($cartItem, $rowId) {
return $cartItem->id == 1;
});
search
方法是 Laravel 的 filter method on collections 的包装器。
闭包只是一个传递给函数的函数,该函数可能会或可能不会从父函数接收变量作为参数。
如果你想在闭包中使用 $request
变量,你必须 use()
它:
Cart::search(function($cartItem, rowId) use($request) {
return $cartItem->id == $request->id;
});
当然,在尝试访问 属性 之前,请确保 $request->has('id')
。
希望这对某人也有帮助。
尝试通过 where()
函数查找 -
Cart::content()->where('id', $id);
其中 id
是您要搜索的项目 ID。
找到它here。
尝试:
$cart $item = Cart::content()->where('rowId', $id)->first();
如果您有多个购物车实例:
$cart_item = Cart::instance('instance')->content()->where('rowId', $id)->first();
如果您想根据购物车项目选项进行搜索:
$cart_item = Cart::content()->where('options', function($q){
$q->where('name', 'simon');
})->first();
我正在尝试使用 Crinsane Larvel Shopping Cart 从购物车中获取商品。 该文档讨论了使用闭包。我不明白所提供的示例。
我正在使用 Laravel 5.3,我正在尝试使用 Request 对象中的信息从购物车中搜索商品。这是我的代码:
查看
@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::get('/cart', 'CartController@getCart');
控制器
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);
}
}
购物车内容合集
`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
}
]
}`
如何使用 Cart::search()
找到 ID 为 1 的购物车商品(香蕉)?
你可以这样做:
Cart::search(function($cartItem, $rowId) {
return $cartItem->id == 1;
});
search
方法是 Laravel 的 filter method on collections 的包装器。
闭包只是一个传递给函数的函数,该函数可能会或可能不会从父函数接收变量作为参数。
如果你想在闭包中使用 $request
变量,你必须 use()
它:
Cart::search(function($cartItem, rowId) use($request) {
return $cartItem->id == $request->id;
});
当然,在尝试访问 属性 之前,请确保 $request->has('id')
。
希望这对某人也有帮助。
尝试通过 where()
函数查找 -
Cart::content()->where('id', $id);
其中 id
是您要搜索的项目 ID。
找到它here。
尝试:
$cart $item = Cart::content()->where('rowId', $id)->first();
如果您有多个购物车实例:
$cart_item = Cart::instance('instance')->content()->where('rowId', $id)->first();
如果您想根据购物车项目选项进行搜索:
$cart_item = Cart::content()->where('options', function($q){
$q->where('name', 'simon');
})->first();