简体   繁体   English

Laravel关系“没有模型”

[英]Laravel relation “without model”

Short explanation 简短说明

I have barcode which contains data about inventory shelf. 我有条形码,其中包含有关库存货架的数据。 I have multiple shelfs and don't want to create own database record for every shelf place and level. 我有多个架子,并且不想为每个架子位置和级别创建自己的数据库记录。 Instead of this, I have records about how many places (aka columns) and levels (aka rows) each shelf contains. 取而代之的是,我有关于每个架子包含多少个位置(又称列)和级别(又称行)的记录。

Is it possible to resolve and return this kind of custom model by searching with barcode? 通过使用条形码搜索可以解决并返回这种自定义模型吗?

More detailed explanation 更详细的解释

I have scannable barcodes attached in every inventory shelf. 我在每个库存货架上都有可扫描的条形码。 Barcode contains data about shelf, place and level. 条形码包含有关架子,位置和水平的数据。

In database, I have model Shelf which have integer attributes "places" and "levels". 在数据库中,我有模型架,其具有整数属性“位置”和“级别”。

In following example I have 3 shelves. 在下面的示例中,我有3个货架。 Shelf A is the biggest one which have 8 storing places on every 4 levels. 货架A是最大的货架,每4层有8个储物空间。 Shelf B is a bit smaller and the shelf C is the smallest containing only 2 places and only one level. 架子B小一点,架子C最小,只有2个地方,只有一层。

| Id | Name | Places | Levels |   
|----|------|--------|--------|
| 1  | A    | 8      | 4      |
| 2  | B    | 5      | 4      |
| 3  | C    | 2      | 1      |

Another model Package belongs to those shelves with pivot. 另一个模型Package属于那些带有支点的货架。 Package can be in shelf A, place 1 and level 2. 包装可以放在架子A的1层和2层。

{
    id: 1,
    shelf_id: 1,
    receiver_id: 123,
    pivot: {
        place: 1,
        level: 2
    },

    id: 2,
    shelf_id: 1,
    receiver_id: 567,
    pivot: {
        place: 1,
        level: 2
    },

}

Now when I search with barcode data "A-1-2" (barcode data: SHELF-AREA-LEVEL ) I would get packages with ids 1 and 2. 现在,当我用条形码数据“ A-1-2”(条形码数据: SHELF-AREA-LEVEL )搜索时,我会得到ID为1和2的包装。

{
    id: 1,
    name: 'A',
    place: 1,
    level: 2,
    packages: [
        {
            id: 1,
            receiver_id: 123,
        },
        {
            id: 2,
            receiver_id: 456,
        }
    ]
}

Is this possible or is it better to do one record for each place? 这可能吗?还是为每个位置做一个记录更好?

I think it depends on what further requirements you have on the software. 我认为这取决于您对该软件有什么进一步的要求。 From what I see, you can have multiple packages within a place in a shelf. 据我所知,您可以在一个架子的某个位置放置多个包装。 If you store the number of places and levels within the Shelf model, you assume that all levels and places within that shelf share the same attributes (eg height, width, etc.). 如果您在Shelf模型中存储位置和层的数量,则假定该Shelf中的所有层和位置都具有相同的属性(例如,高度,宽度等)。 If that is indeed the case, I think you can go with your proposed model. 如果确实如此,我认为您可以采用建议的模型。

But you are now storing the location of a package in a pivot - this would only make sense if you want to have multiple entries for the same package and shelf, eg if you move the package to another shelf and you want to keep the old information. 但是,您现在将数据包的位置存储在枢轴中-仅当您想为同一个数据包和货架有多个条目时才有意义,例如,如果您将包裹移动到另一个货架并希望保留旧信息。 If you don't need the "location history", you could put the level and place within the Package model instead of in a pivot. 如果不需要“位置历史记录”,则可以将级别和位置放在Package模型中,而不是放在透视图中。

If you want to go full "relational MySQL style", I would create different models for each property. 如果要使用完整的“关系MySQL样式”,我将为每个属性创建不同的模型。 A Package should be in a Place , a Place lies within a Level and a Level belongs to a Shelf , giving you the following structure Package应该在一个Place ,一个Place在一个Level ,一个Level属于一个Shelf ,为您提供以下结构

Shelf (id, name)
Level (id, shelf_id)
Place (id, level_id)
Package (id, place_id)

That way you could store additional information for each Level / Place. 这样,您可以为每个级别/位置存储其他信息。 But it depends on your requirements and what your software should be able to accomplish. 但这取决于您的要求以及您的软件应该能够完成的工作。

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

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