Mysql 从表中检索除laravel中的少数行以外的所有行
我正在使用Laravel5.1和MySQL作为后端来服务移动应用程序发出的REST-API请求 我有一个购物车表和物品表。所以,每当用户在他的移动应用程序中进入“项目屏幕”时,在后端我都应该执行两项任务 首先检查他的购物车中是否有任何物品。如果是,即购物车表中有项目,则从购物车表中取出这些项目,从项目表中取出其余项目 如果购物车中没有商品,那么我将轻松地从items表中获取所有商品,并将其显示给用户 我无法执行任务1。因为我首先从购物车表中检索所有的商品标识。它将返回一个集合。现在,我应该检查购物车表中的这些项目ID是否存在于项目表中。如果是,则不从items表中获取这些项,而是从items表中获取所有其他项。将项目表中的项目与购物车表中的项目组合起来,并将其显示给用户。我怎样才能做到这一点 目前,问题是,我正在从购物车表中获取所有的“item_ID”。它返回项目ID的集合。使用foreach循环,对于每个item_id,我查询Items表,如下所示:Mysql 从表中检索除laravel中的少数行以外的所有行,mysql,laravel,laravel-4,laravel-5,laravel-5.1,Mysql,Laravel,Laravel 4,Laravel 5,Laravel 5.1,我正在使用Laravel5.1和MySQL作为后端来服务移动应用程序发出的REST-API请求 我有一个购物车表和物品表。所以,每当用户在他的移动应用程序中进入“项目屏幕”时,在后端我都应该执行两项任务 首先检查他的购物车中是否有任何物品。如果是,即购物车表中有项目,则从购物车表中取出这些项目,从项目表中取出其余项目 如果购物车中没有商品,那么我将轻松地从items表中获取所有商品,并将其显示给用户 我无法执行任务1。因为我首先从购物车表中检索所有的商品标识。它将返回一个集合。现在,我应该检查购
("*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循环中的每个新迭代替换集合
以下是my CartController中的函数:
请帮我解决这个问题,TIA。考虑在代码中添加以下内容 尝试后添加此项
$getCartItemsIds = UserCartDetailBng::where('uuid','=',$uuid)->lists('id');
从else语句中删除foreach块和$getItemId,然后使用
$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->whereNotIn($getCartItemsIds)->get();
请注意,$itemDetails有两种不同的检索方法。请编辑第二种方法以同时使用->whereNotIn$getCartItemsID
最后,根据您的意图,您应该编辑返回的$数据,以包括购物车上的内容
$data = array('count'=>$count,'result'=>$itemDetails, 'cart'=>$getCartItems);
考虑将以下内容添加到代码中 尝试后添加此项
$getCartItemsIds = UserCartDetailBng::where('uuid','=',$uuid)->lists('id');
从else语句中删除foreach块和$getItemId,然后使用
$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->whereNotIn($getCartItemsIds)->get();
请注意,$itemDetails有两种不同的检索方法。请编辑第二种方法以同时使用->whereNotIn$getCartItemsID
最后,根据您的意图,您应该编辑返回的$数据,以包括购物车上的内容
$data = array('count'=>$count,'result'=>$itemDetails, 'cart'=>$getCartItems);
考虑到您的模型设置正确,请尝试此代码
//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);
}
考虑到您的模型设置正确,请尝试此代码
//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);
}