检索 table 中的所有行,laravel 中的几行除外
Retrieve all rows from table except few rows in laravel
我正在使用 Laravel 5.1 和 MySQL 作为后端来处理来自移动应用程序的 REST-API 请求。
我有一个 购物车 table 和 商品 table。因此,每当用户在他的移动应用程序中访问 'Items screen' 时,我应该在后端执行 2 个任务。
首先检查他的购物车中是否有任何物品。如果 yes(即购物车 table 中有商品),则从 Cart table 中获取这些商品,然后项 table.
的剩余项
如果购物车中没有没有件商品,那么我会轻松地从商品table中取出所有商品并显示给用户。
我无法执行任务 1。因为我首先从 购物车 [=86] 中检索所有 item_ids =]。它会return一个集合。现在我应该检查 item_ids(来自购物车 table)是否存在于 商品 table 中。如果是,请不要从 Items table 中获取这些项目,而是从 Items table 中获取所有其他项目。合并 商品 table 中的商品和 购物车 table 中的商品,并将其显示给用户。我怎样才能做到这一点?
目前,问题是,我正在从 购物车 table 中获取所有 'item_ids'。它 return 是项目 ID 的集合。使用 foreach()
循环,对于每个 item_id,我查询 Items table 如下:
("*where('item_id','!=',$getItemId)->get();*")
$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->where('item_id','!=',$getItemId)->get();
它 return 对每个单独的项目进行集合检查。并用 foreach 循环中的每个新迭代替换集合。
这是我的 CartController 中的函数:
public function getItemsWithCartItems($uuid,$store_id,$category_id,$subcategory_id,$subsubcategory_id=null)
{
try
{
$getCartItems = UserCartDetailBng::where('uuid','=',$uuid)->get();
if($getCartItems->isEmpty()) //Performing task 2. No problem here.
{
if($subsubcategory_id==null || $subsubcategory_id==0) // Bcoz, subsubcategory_id is optional
{
$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->get();
if($itemDetails->isEmpty()){
return ResponseService::buildFailureResponse("Store Item not found");
}
}
else
{
$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->where('subsubcategory_id','=',$subsubcategory_id)->get();
if($itemDetails->isEmpty()){
return ResponseService::buildFailureResponse("Store Item not found");
}
}
$count = $itemDetails->count();
$data = array('count'=>$count,'result'=>$itemDetails);
return ResponseService::buildSuccessResponse($data);
}
else //Performing task 1. Here is the problem.
{
// I am using "where('item_id','!=',$getItemId)->get()" And it returns collection checking against each individual item. and replaces collection with every new iteration in foreach loop.
foreach($getCartItems as $getCartItem)
{
$getItemId = $getCartItem->id;
if($subsubcategory_id==null || $subsubcategory_id==0)
{
$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->where('item_id','!=',$getItemId)->get();
if($itemDetails->isEmpty()){
return ResponseService::buildFailureResponse("Store items not found");
}
}
else
{
$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->where('subsubcategory_id','=',$subsubcategory_id)->where('item_id','!=',$getItemId)->get();
if($itemDetails->isEmpty()){
return ResponseService::buildFailureResponse("Store items not found");
}
}
$count = $itemDetails->count();
$data = array('count'=>$count,'result'=>$itemDetails);
return ResponseService::buildSuccessResponse($data);
}
}
}
请帮我解决这个问题,TIA。
考虑将以下内容添加到您的代码中
尝试后添加
$getCartItemsIds = UserCartDetailBng::where('uuid','=',$uuid)->lists('id');
从 else 语句中删除 foreach 块和 $getItemId,然后使用
检索 $itemdetails
$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->whereNotIn($getCartItemsIds)->get();
请注意,您对 $itemDetails 有两种不同的检索方式,编辑第二个检索方式也使用 ->whereNotIn($getCartItemsIds)
最后,根据您的意图,您应该编辑返回的 $data 以包括购物车中的商品。
$data = array('count'=>$count,'result'=>$itemDetails, 'cart'=>$getCartItems);
考虑到您的模型设置正确,请尝试此代码
//get the cart items with the item
$cartItems = UserCartDetailBng::where('uuid','=',$uuid)->with('itemsBng')->get();
if($cartItems->isEmpty())
{
//
}
else
{
//find items excluding of cart items
$itemDetails = ItemBng::where('store_id','=',$store_id)
->where('category_id','=',$category_id)
->where('subcategory_id','=',$subcategory_id)
->whereNotIn('id', $cartItems->lists('item_id'))->get();
$data = array('cartItems'=>$cartItems,'generalItems'=>$itemDetails);
return ResponseService::buildSuccessResponse($data);
}
我正在使用 Laravel 5.1 和 MySQL 作为后端来处理来自移动应用程序的 REST-API 请求。
我有一个 购物车 table 和 商品 table。因此,每当用户在他的移动应用程序中访问 'Items screen' 时,我应该在后端执行 2 个任务。
首先检查他的购物车中是否有任何物品。如果 yes(即购物车 table 中有商品),则从 Cart table 中获取这些商品,然后项 table.
的剩余项
如果购物车中没有没有件商品,那么我会轻松地从商品table中取出所有商品并显示给用户。
我无法执行任务 1。因为我首先从 购物车 [=86] 中检索所有 item_ids =]。它会return一个集合。现在我应该检查 item_ids(来自购物车 table)是否存在于 商品 table 中。如果是,请不要从 Items table 中获取这些项目,而是从 Items table 中获取所有其他项目。合并 商品 table 中的商品和 购物车 table 中的商品,并将其显示给用户。我怎样才能做到这一点?
目前,问题是,我正在从 购物车 table 中获取所有 'item_ids'。它 return 是项目 ID 的集合。使用 foreach()
循环,对于每个 item_id,我查询 Items table 如下:
("*where('item_id','!=',$getItemId)->get();*")
$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->where('item_id','!=',$getItemId)->get();
它 return 对每个单独的项目进行集合检查。并用 foreach 循环中的每个新迭代替换集合。
这是我的 CartController 中的函数:
public function getItemsWithCartItems($uuid,$store_id,$category_id,$subcategory_id,$subsubcategory_id=null)
{
try
{
$getCartItems = UserCartDetailBng::where('uuid','=',$uuid)->get();
if($getCartItems->isEmpty()) //Performing task 2. No problem here.
{
if($subsubcategory_id==null || $subsubcategory_id==0) // Bcoz, subsubcategory_id is optional
{
$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->get();
if($itemDetails->isEmpty()){
return ResponseService::buildFailureResponse("Store Item not found");
}
}
else
{
$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->where('subsubcategory_id','=',$subsubcategory_id)->get();
if($itemDetails->isEmpty()){
return ResponseService::buildFailureResponse("Store Item not found");
}
}
$count = $itemDetails->count();
$data = array('count'=>$count,'result'=>$itemDetails);
return ResponseService::buildSuccessResponse($data);
}
else //Performing task 1. Here is the problem.
{
// I am using "where('item_id','!=',$getItemId)->get()" And it returns collection checking against each individual item. and replaces collection with every new iteration in foreach loop.
foreach($getCartItems as $getCartItem)
{
$getItemId = $getCartItem->id;
if($subsubcategory_id==null || $subsubcategory_id==0)
{
$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->where('item_id','!=',$getItemId)->get();
if($itemDetails->isEmpty()){
return ResponseService::buildFailureResponse("Store items not found");
}
}
else
{
$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->where('subsubcategory_id','=',$subsubcategory_id)->where('item_id','!=',$getItemId)->get();
if($itemDetails->isEmpty()){
return ResponseService::buildFailureResponse("Store items not found");
}
}
$count = $itemDetails->count();
$data = array('count'=>$count,'result'=>$itemDetails);
return ResponseService::buildSuccessResponse($data);
}
}
}
请帮我解决这个问题,TIA。
考虑将以下内容添加到您的代码中
尝试后添加
$getCartItemsIds = UserCartDetailBng::where('uuid','=',$uuid)->lists('id');
从 else 语句中删除 foreach 块和 $getItemId,然后使用
检索 $itemdetails$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->whereNotIn($getCartItemsIds)->get();
请注意,您对 $itemDetails 有两种不同的检索方式,编辑第二个检索方式也使用 ->whereNotIn($getCartItemsIds)
最后,根据您的意图,您应该编辑返回的 $data 以包括购物车中的商品。
$data = array('count'=>$count,'result'=>$itemDetails, 'cart'=>$getCartItems);
考虑到您的模型设置正确,请尝试此代码
//get the cart items with the item
$cartItems = UserCartDetailBng::where('uuid','=',$uuid)->with('itemsBng')->get();
if($cartItems->isEmpty())
{
//
}
else
{
//find items excluding of cart items
$itemDetails = ItemBng::where('store_id','=',$store_id)
->where('category_id','=',$category_id)
->where('subcategory_id','=',$subcategory_id)
->whereNotIn('id', $cartItems->lists('item_id'))->get();
$data = array('cartItems'=>$cartItems,'generalItems'=>$itemDetails);
return ResponseService::buildSuccessResponse($data);
}