繁体   English   中英

从表中检索除laravel中的几行之外的所有行

[英]Retrieve all rows from table except few rows in laravel

我使用 Laravel 5.1 和 MySQL 作为后端来处理从移动应用程序发出的 REST-API 请求。

我有一个Cart 表Items 表 所以每当用户在他的移动应用程序中进入“项目屏幕”时,在后端我应该执行 2 个任务。

  1. 首先检查他的购物车中是否有任何物品。 如果(即 Cart 表中有项目),则从Cart 表中获取这些项目,从Item 表中获取其余项目。

  2. 如果 Cart 中没有项目,那么我将轻松地从 Items 表中获取所有项目并将其显示给用户。

我被困在无法执行任务 1. 因为我首先从Cart 表中检索所有item_ids 它将返回一个集合。 现在我应该检查这些 item_ids(来自购物车表)是否存在于Items 表中 如果是的话,不取从项目表中的项目,但取从项目表中的所有其他项目。 Items 表中的这些项目和Cart 表中的项目组合起来,并将其显示给用户。 我怎样才能做到这一点?

目前,问题是,我从Cart 表中获取了所有“item_ids” 它返回项目 ID 的集合。 使用foreach()循环,对于每个item_id ,我查询Items 表如下:

("*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();

它返回针对每个单独项目的集合检查。 并用 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。

考虑到您的模型设置正确,请尝试使用此代码

       //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);
       }

考虑将以下内容添加到您的代码中

尝试后添加这个

$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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM