signifly/laravel-cancellation

一个 Laravel 取消订单的包。

v1.5.0 2021-03-29 08:31 UTC

This package is auto-updated.

Last update: 2024-08-29 05:07:48 UTC


README

Latest Version on Packagist Tests StyleCI Quality Score Total Downloads

signifly/laravel-cancellation 包允许您轻松处理模型取消操作。它受到 Laravel 中 SoftDeletes 实现的启发。

开始使用只需做以下操作

// 1. Add cancelled_at column to your table by using our macro cancellable
Schema::create('orders', function (Blueprint $table) {
    // ...
    $table->cancellable();
    // ...
});

// 2. Add the Cancellable trait to your model
class Order extends Model
{
    use Cancellable;
}

以下是添加特性后如何使用的一个小演示

$order = Order::find(1);
$order->cancel();

您可以查询已取消的实体

$orders = Order::onlyCancelled()->get(); // returns all the cancelled entities
$orders = Order::withCancelled()->get(); // includes cancelled entities
$orders = Order::withoutCancelled()->get(); // excludes cancelled entities

其他有用方法

$order->isCancelled(); // returns true or false
$order->keep(); // keep a cancelled order

Order::whereKey([1, 2, 3])->keep(); // keeps orders with ids: 1, 2, 3

安装

您可以通过 composer 安装此包

$ composer require signifly/laravel-cancellation

该包将自动注册自己。

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

$ php artisan vendor:publish --provider="Signifly\Cancellation\CancellationServiceProvider" --tag="config"

注意:如果您在配置中将 exclude 变量设置为 true,则默认情况下查询结果将不包括已取消的结果(类似于 SoftDeletes)。

这是发布配置文件的包含内容

return [
    /**
     * Exclude the cancellations from the model's queries.
     * Will apply to all, find, etc.
     */
    'exclude' => false,
];

测试

$ composer test

安全

如果您发现任何安全问题,请通过电子邮件 dev@signifly.com 联系我们,而不是使用问题跟踪器。

致谢

许可协议

MIT 许可协议(MIT)。有关更多信息,请参阅 许可文件