laravel 中库存系统的数据库设计

Database design for an inventory system in laravel

我想使用 laravel 创建一个基本的库存系统。我有 2 tables。一个用于采购,另一个用于销售。 购买字段 table:id,product_id,quantity_bought,date_bought。

销售中的字段 table:id,product_id,quantity_sold,date_sold

我将如何执行验证以使 quantity_sold 不能大于 quantity_bought?另外我如何获得 quantity_remaining 并将其保存在数据库中。

也许你应该添加另一个 table products 来保存当前库存量。

然后您可以在 productspurchases 以及 sales 之间创建 Eloquent 关系。因此 purchasessales 中的 product_id 列指向产品 table。有关详细信息,请参阅:https://laravel.com/docs/5.3/eloquent-relationships

然后,一旦您需要添加新的销售额,您可以执行一个事务,在该事务中检查产品的当前库存量是否高于产品的销售量。如果一切顺利,您将更新当前库存量并为销售额创建新条目 table。如果任何检查失败,只需回滚事务,如果一切正常,则提交它。有关交易的更多信息,请参阅:https://laravel.com/docs/5.3/database#database-transactions

这样 products table 的 amount_in_stock 将始终拥有当前库存量,而您的 purchasessales tables 将是您的日志。


回到验证,在此设置中,您需要查询 products table 以获取当前库存量。您可以从数据库中获取它并将其与您输入的销售额进行比较(无论如何您都应该这样做)。没有开箱即用的 Laravel 验证规则可以检查 table x 的值并将其与某处的 table 列进行比较。当然你可以创建自己的,但我真的不明白为什么你应该在这里这样做。无论如何,请查看:https://laravel.com/docs/5.3/validation#custom-validation-rules 了解更多信息。

您可以通过在 "Purchase" 模型中定义 totalBoughts($product_id) 并在 "Sales" table 中定义 totalSolds($product_id) 并通过以下方式调用控制器产品编号

but this is not an efficient way to do so

或者,您可以使用股票 table 在这两者之间建立关系,您可以在其中存储产品的当前状态

when purchasing the stock incremented and for sale it decrease the amount of products so u cannot manually check the entire table for availability if any product in stock is set to 0 then it can't be sold

Laravel 有一个很好的库存库。同时支持 4 和 5 https://github.com/stevebauman/inventory

根据图书馆的表格如下

+---------------------------------+
|Table Names                      |
+---------------------------------+
| categories                      |
| inventories                     |
| inventory_skus                  |
| inventory_stock_movements       |
| inventory_stocks                |
| inventory_suppliers             |
| inventory_transaction_histories |
| inventory_transactions          |
| locations                       |
| metrics                         |
| migrations                      |
| password_resets                 |
| suppliers                       |
| users                           |
+---------------------------------+