fabpl/laravel-stock

为 Eloquent 模型保持库存。

1.0 2022-11-22 13:16 UTC

This package is auto-updated.

Last update: 2024-09-22 17:52:10 UTC


README

为 Eloquent 模型保持库存。此包将跟踪模型库存变更。您可以增加或减少库存。

安装

您可以通过 composer 安装此包

composer require fabpl/laravel-stock

您可以使用以下命令发布和运行迁移

php artisan vendor:publish --tag="stock-migrations"
php artisan migrate

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="stock-config"

这是已发布配置文件的内容

return [

    'models' => [

        /*
         * When using the "InteractsWithStock" trait from this package, we need to know which
         * Eloquent model should be used to retrieve your stocks. Of course, it
         * is often just the "Stock" model, but you may use whatever you like.
         *
         * The model you want to use as a Permission model needs to implement the
         * `Fabpl\Stock\Contracts\Permission` contract.
         */

        'stock' => Fabpl\Stock\Models\Stock::class,

        /*
         * When using "InteractsWithStockMutations" or "ReferencesInStockMutations" traits from this package, we need to know which
         * Eloquent model should be used to retrieve your stock mutations. Of course, it
         * is often just the "StockMutation" model, but you may use whatever you like.
         *
         * The model you want to use as a StockMutation model needs to implement the
         * `Fabpl\Stock\Contracts\StockMutation` contract.
         */

        'stock_mutation' => Fabpl\Stock\Models\StockMutation::class,

    ],

    'table_names' => [

        /*
         * When using the "InteractsWithStock" trait from this package, we need to know which
         * table should be used to retrieve your stocks. We have chosen a basic
         * default value, but you may easily change it to any table you like.
         */

        'stocks' => 'stocks',

        /*
         * When using "InteractsWithStockMutations" or "ReferencesInStockMutations" traits from this package, we need to know which
         * table should be used to retrieve your stock mutations. We have chosen a basic
         * default value, but you may easily change it to any table you like.
         */

        'stock_mutations' => 'stock_mutations',
    ],

];

准备您的模型

要将库存与模型关联,模型必须实现以下接口和特性

use \Fabpl\Stock\Concerns\InteractsWithStock;
use \Fabpl\Stock\Contract\HasStock;

class Product extends Model implements HasStock
{
    use InteractsWithStock;
}

要增加库存,您可以使用 incrementStock 方法

$product->incrementStock(10);

要减少库存,您可以使用 decrementStock 方法

$product->decrementStock(10);

如果您想使用 引用 功能,模型必须实现以下接口和特性

use \Fabpl\Stock\Concerns\ReferencesInStockMutations;
use \Fabpl\Stock\Contract\CauseStockMutation;

class Order extends Model implements CauseStockMutation
{
    use ReferencesInStockMutations;
}

测试

composer test

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

贡献

请参阅 CONTRIBUTING 了解详细信息。

安全漏洞

请查看 我们的安全策略 了解如何报告安全漏洞。

鸣谢

许可

MIT 许可证 (MIT)。请参阅 许可文件 了解更多信息。