检索表格中的所有行,除了laravel中的几行

时间:2015-09-24 05:09:57

标签: mysql laravel laravel-4 laravel-5 laravel-5.1

我正在使用Laravel 5.1& MySQL作为后端来提供从移动应用程序发出的REST-API请求。

我有购物车表商品表。因此,每当用户访问移动应用程序中的' Items屏幕' 时,在后端我应该执行2个任务。

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

  2. 如果购物车中有没有项目,那么我会轻松从Items表中提取所有商品并将其显示给用户。

  3. 我无法执行任务1.因为我首先从购物车表中检索所有 item_ids 。它将返回一个集合。现在我应该检查项目表中是否存在这些item_ids(来自购物车表)。如果是,请不要从项目表中获取这些项目,但是从项目表中获取所有其他项目。将这些项目与项目表&amp;来自购物车表的商品并将其展示给用户。我怎样才能做到这一点?

    目前,问题是,我从购物车表获取所有&#39; item_ids&#39; 。它返回item-id的集合。使用foreach()循环,对于每个 item_id ,我正在查询项目表,如下所示:

    ("*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。

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:2)

考虑在代码中添加以下内容

尝试后添加此内容

$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 = array('count'=>$count,'result'=>$itemDetails, 'cart'=>$getCartItems);