简体   繁体   中英

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.

I have a Cart table and Items table . So whenever user comes to 'Items screen' in his mobile App, In the backend I should perform 2 tasks.

  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 .

  2. If there are no items in Cart, then I will easily fetch all items from the Items table and show it to user.

I am stuck not being able to perform the task 1. Because I am first retrieving all the item_ids from the Cart table . It will return a collection. Now I should check if these item_ids(from cart table) are present in Items table . 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. How can I achieve this?

Currently, problem is, I am getting all 'item_ids' from Cart table . It returns a collection of item-ids. Using foreach() loop, for every item_id , I am querying Items table as follows:

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

Here is the function in my 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.

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

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

Lastly as per your intention you should edit the $data returned to include also what is on the cart.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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