[英]How to retrieve few random rows of left table and all the related rows from right table in 1 to n relationship
[英]Retrieve all rows from table except few rows in laravel
我使用 Laravel 5.1 和 MySQL 作為后端來處理從移動應用程序發出的 REST-API 請求。
我有一個Cart 表和Items 表。 所以每當用戶在他的移動應用程序中進入“項目屏幕”時,在后端我應該執行 2 個任務。
首先檢查他的購物車中是否有任何物品。 如果是(即 Cart 表中有項目),則從Cart 表中獲取這些項目,從Item 表中獲取其余項目。
如果 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.