简体   繁体   English

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

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

I am using Laravel 5.1 & MySQL as backend to serve REST-API requests made from the mobile app.我使用 Laravel 5.1 和 MySQL 作为后端来处理从移动应用程序发出的 REST-API 请求。

I have a Cart table and Items table .我有一个Cart 表Items 表 So whenever user comes to 'Items screen' in his mobile App, In the backend I should perform 2 tasks.所以每当用户在他的移动应用程序中进入“项目屏幕”时,在后端我应该执行 2 个任务。

  1. First check if any Items are there in his cart.首先检查他的购物车中是否有任何物品。 If yes (ie,there are items in Cart table), then fetch those items from Cart table and the remaining items from Item table .如果(即 Cart 表中有项目),则从Cart 表中获取这些项目,从Item 表中获取其余项目。

  2. If there are no items in Cart, then I will easily fetch all items from the Items table and show it to user.如果 Cart 中没有项目,那么我将轻松地从 Items 表中获取所有项目并将其显示给用户。

I am stuck not being able to perform the task 1. Because I am first retrieving all the item_ids from the Cart table .我被困在无法执行任务 1. 因为我首先从Cart 表中检索所有item_ids It will return a collection.它将返回一个集合。 Now I should check if these item_ids(from cart table) are present in Items table .现在我应该检查这些 item_ids(来自购物车表)是否存在于Items 表中 If yes, don't fetch those items from Items table , BUT fetch all other items from Items table .如果是的话,不取从项目表中的项目,但取从项目表中的所有其他项目。 Combine those items from Items table & items from Cart table and show it to user.Items 表中的这些项目和Cart 表中的项目组合起来,并将其显示给用户。 How can I achieve this?我怎样才能做到这一点?

Currently, problem is, I am getting all 'item_ids' from Cart table .目前,问题是,我从Cart 表中获取了所有“item_ids” It returns a collection of item-ids.它返回项目 ID 的集合。 Using foreach() loop, for every item_id , I am querying Items table as follows:使用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();

And it returns collection checking against each individual item.它返回针对每个单独项目的集合检查。 and replaces collection with every new iteration in foreach loop.并用 foreach 循环中的每个新迭代替换集合。

Here is the function in my CartController :这是我的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);
            }
        }
    }

please help me solve this problem, TIA.请帮我解决这个问题,TIA。

Considering your models are set correctly, try this code考虑到您的模型设置正确,请尝试使用此代码

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

Consider adding the following to your code考虑将以下内容添加到您的代码中

Add this after try尝试后添加这个

$getCartItemsIds = UserCartDetailBng::where('uuid','=',$uuid)->lists('id');

Remove the foreach block and $getItemId from the else statement then have the $itemdetails retrieved using从 else 语句中删除 foreach 块和 $getItemId 然后使用检索 $itemdetails

$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->whereNotIn($getCartItemsIds)->get();

please note you have two different retrievals for $itemDetails edit the second one to also use the ->whereNotIn($getCartItemsIds)请注意,您对 $itemDetails 有两种不同的检索,编辑第二个也可以使用->whereNotIn($getCartItemsIds)

Lastly as per your intention you should edit the $data returned to include also what is on the cart.最后,根据您的意图,您应该编辑返回的 $data 以包括购物车上的内容。

$data = array('count'=>$count,'result'=>$itemDetails, 'cart'=>$getCartItems);

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

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