繁体   English   中英

在Laravel中查询此内容的最佳方法

[英]Best Way to Query This in Laravel

我将尝试尽可能简化此过程,以便直截了当。 如果您还有其他问题,请务必与我联系。

我有2张桌子,如下所示:

item_centers:

| 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)。 除了该下拉菜单,我还有一个项目下拉列表,其中列出了包含“项目”表中关键字的所有唯一说明。 在这2个下拉菜单下,我有一个文本框,允许用户输入他们要从所选“项目”中减去的数量。

当用户选择item_center,项目说明并单击Submit时,我将执行以下操作:1.从“ items”表中获取所有项目,并使用与从“ item center”下拉列表中选择的项目相同的“ identifier” 。 2.在步骤1中汇总所有已检索项目的数量。 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'));
}

就像我之前说过的,这确实简化了流程-事情本来可以忽略的。 因此,请让我知道是否有任何混淆。

我认为,做到这一点的最佳方法是使用laravel关系(如果不是这样),就像这样

ItemCenter模型

public function items()
{
    return $this->hasMany(Item::class);
}

物品模型

public function itemCenter()
{
    return $this->belongsTo(ItemCenter::class);
}

因此,现在在表上,如果两个表上的identifer和description列相同,则可以删除它们,并在item表上使用item_center_id foriegn键替换它们,以替换item_centers表的id列。

现在简化查询,我想这会是这样

$item_centers = ItemCenter::with(['items' => function($query){
   $query->havingRaw('SUM(quantity) <= 0 ');
}])->get();

使用下面的查询,您基本上不再需要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”)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM