使用雄辩获取所有未包含的项目

发布于 2025-01-29 10:45:47 字数 234 浏览 0 评论 0原文

我有以下表:

orders:
- id
- date

item_order:
- order_id
- item_id

items:
- id
- desc
- price

使用雄辩,我如何获取所有未包含在给定订单中的项目(例如,带有id = 6的订单)?

我正在尝试建立关系&亚Queries,但没有运气。

提前致谢。

I have the following tables:

orders:
- id
- date

item_order:
- order_id
- item_id

items:
- id
- desc
- price

Using Eloquent, how can I get all the items NOT included in a giving order (say, the order with id = 6)?

I'm trying to do relationships & subqueries, but without luck.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮年 2025-02-05 10:45:47

您的问题还不够清楚,使用雄辩,我如何获取所有未包含的项目(例如,ID = 6的订单)?,此句子尚不清楚。

另一方面,基于您的说明,下面的示例将返回带有其项目的订单列表,除了您不想要的内容:

$id = 6;

// Get list of orders with itemOrder.
Order::with('itemOrder' => function ($query) use $($id) {
    //make sure query don't return list of order items belong to order id = 6.
    $query->where('order_id', '!=,' $id)
})->get();

您将获得订单的列表,带有item_order但您将不会获得item_order,用于订单,ID为6。

如果不是您所要求的,请澄清并更新您的问题以更好地说明。

根据您的评论:您的解决方案给了我订单,但是我想要所有项目,除了具有ID = 6的订单外,您可以简单地用指定的模型和关系替换查询

$id = 6;
Item::with('itemOrder' => function ($query) use $($id) {
    $query->where('order_id', '!=,' $id)
})->get();

// Or,
// This one only return items with itemOrders and of course returned itemsOrder
// will be affected by query inside callback function
Item::whereHas('itemOrder' => function ($query) use $($id) {
    $query->where('order_id', '!=,' $id)
})->get();

Your question is not clear enough, Using Eloquent, how can I get all the items NOT included in a giving order (say, the order with id = 6)?, this sentence is unclear.

In other hand based on your explanation, below example gonna return list of orders with their items, except something you don't want:

$id = 6;

// Get list of orders with itemOrder.
Order::with('itemOrder' => function ($query) use $($id) {
    //make sure query don't return list of order items belong to order id = 6.
    $query->where('order_id', '!=,' $id)
})->get();

You will get list of orders with item_order but you will not get item_order for order with id of 6.

If this is not what you've asked please clarify and update your question for better explanation.

Based on your comment : Your solution gives me the ORDERS, but I want all the ITEMS, except those of the order with id = 6., you can simply replace query with specified model and relations

$id = 6;
Item::with('itemOrder' => function ($query) use $($id) {
    $query->where('order_id', '!=,' $id)
})->get();

// Or,
// This one only return items with itemOrders and of course returned itemsOrder
// will be affected by query inside callback function
Item::whereHas('itemOrder' => function ($query) use $($id) {
    $query->where('order_id', '!=,' $id)
})->get();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文