mercredi 23 septembre 2015

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 (i.e.,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 struck by not 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 quering 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.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire