首页 > 解决方案 > 在 Laravel 中查询这个的最佳方法

问题描述

我将尝试尽可能简化此过程以直截了当。如果您有任何其他问题,请务必让我知道。

我有2个表如下:

项目中心:

| id | center | identifier | description        |
|----|--------|------------|--------------------|
| 1  | 1A     |       0011 | Description for 1A |
| 2  | 1B     |       0022 | Description for 1B |
| 3  | 1C     |       0033 | Description for 1C |

一个项目中心可以有许多项目。表中的“标识符”列,代表下面“项目”表中的标识符列。所以中心 1A 可以有许多标识符为 0011 的“项目”。

项目:

| id | identifier | description        | quantity |
|----|------------|--------------------|----------|
| 1  |       0011 | Description for 1A |      250 |
| 2  |       0022 | Description for 1B |      500 |
| 3  |       0033 | Description for 1C |      750 |

我有一个项目中心下拉列表,其中列出了“item_centers”表中的所有项目中心。(例如:1A)。除了该下拉列表,我还有一个项目下拉列表,其中列出了包含“项目”表中的关键字的所有唯一描述。在这两个下拉菜单下,我有一个文本框,允许用户输入他们试图从所选“项目”中减去的数量。

当用户选择 item_center、item description 并单击提交时 - 我有一个执行此操作的过程: 1. 从“items”表中获取所有项目,与从项目中心下拉列表中选择的项目具有相同的“标识符” . 2. 将第一步中检索到的所有物品的数量加起来。3. 从项目列表中减去用户输入的金额,从最旧的第一个开始(created_at 列)。

所以,对于我的问题...

我们有许多项目中心包含数量为 0 的项目。我希望能够从列表中删除所有数量为 0 的项目中心,这样用户就不必对 100 个项目中心进行排序找到一个数量大于 0 的。

这是我模拟的一个简单示例。这显然是一种可怕的方法,因为它运行了大量的查询——并且会超时。但它可能会更好地作为我在这里想要实现的目标的模型。

public function index()
{
        $itemCenters = ItemCenter::select(['id', 'center', 'identifier', 'description'])
                                    ->orderBy('center', 'asc')
                                    ->orderBy('description', 'asc')
                                    ->get();

        $itemDescriptions = Item::select('description')
                        ->where('description', 'LIKE', '% .$keyword. %')
                        ->orWhere('description', 'LIKE', '.$keyword. %')
                        ->orWhere('description', 'LIKE', '% $.$keyword.')
                        ->distinct('description')
                        ->orderBy('description', 'asc')
                        ->get();

        foreach ($itemCenters as $itemCenter)
        {
            foreach ($itemDescriptions as $itemDescription)
            {
                $currentQty = Item::where('identifier', $itemCenter->identifier)
                                 ->where('description', $itemDescription->description)
                                 ->sum('quantity');


                if ($currentQty <= 0)
                {
                    $itemCenter->forget($itemCenter->id);
                }
            }
        }

        return view('pages.ItemRemoval', compact('itemCenters'), compact('itemDescriptions'));
}

就像我之前说的那样,这确实简化了流程 - 事情可能会被遗漏。因此,如果有任何混淆,请告诉我。

标签: phpsqllaraveleloquent

解决方案


使用下面的查询,您基本上不再需要 foreach 循环来过滤 $itemCenters。零个项目以及相应的 item_centers 已经被过滤掉了。

$itemCenters = DB::table('item_centers')
            ->join('items', 'item_centers.identifier', '=', 'items.identifier')     
            ->select('item_centers.id as id', 'item_centers.center as center', 'item.identifier as identifier', 'item_centers.description as description', 'items.quantity as quantity')
            ->where('quantity', '<>', 0)
            ->orderBy('center', 'asc')
            ->orderBy('description', 'asc')
            ->get();

您可能需要为您的操作选择另一列('items.created_at as created_at')。


推荐阅读