stfn/laravel-pending-updates

v1.0.0 2023-03-03 15:26 UTC

This package is not auto-updated.

Last update: 2024-09-26 19:47:33 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status

在更新 Eloquent 模型时,使用此包可以延迟更新过程一段时间。

$news = News::find(1);

$news->postpone()
    ->startFrom('2023-01-01 00:00:00')
    ->keepForHours(24)
    ->update(['is_active' => true]);

在这种情况下,模型本身不会被更新。该包只会为您安排一个更新。因此,此新闻将在1月1日全天有效,之后,该包将恢复新闻到其先前状态。

安装

您可以通过 composer 安装此包

composer require stfn/laravel-pending-updates

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

php artisan vendor:publish --tag="pending-updates-migrations"
php artisan migrate

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

php artisan vendor:publish --tag="pending-updates-config"

这是发布配置文件的内容

return [
    // Maximum postpone in days.
    'max_postpone_days' => 10,

    // The model uses to store pending updates.
    'model' => \Stfn\PendingUpdates\Models\PendingUpdate::class,
];

当运行控制台命令 pending-updates:check 时,将检查所有挂起的更新,如果需要将某些更新回滚到原始表,此命令将为您执行该操作。

该命令需要安排在 Laravel 控制台内核中。

// app/Console/Kernel.php
use Stfn\PendingUpdates\Commands\CheckPendingUpdates;

protected function schedule(Schedule $schedule)
{
   $schedule->command(CheckPendingUpdates::class)->everyMinute();
}

用法

您应该将 HasPendingUpdates 特性添加到所有需要具有挂起更新选项的模型中。

use Illuminate\Database\Eloquent\Model;
use Stfn\PendingUpdates\Models\Concerns\HasPendingUpdates;

class Ticket extends Model
{
    use HasPendingUpdates;
}

有了这个,您将能够延迟此模型的更新。

使用 keep for

使用 keep for,更新将立即执行,但包将在指定数量的分钟、小时或天后恢复更改。

$ticket = Ticket::find(1);

// Update ticket price to 200 and keep it updated for 60 minutes.
$ticket->postpone()
    ->keepForMinutes(60)
    ->update(['price' => 200]);
    
// Update ticket price to 200 and keep it updated for 12 hours.
$ticket->postpone()
    ->keepForHours(12)
    ->update(['price' => 200]);

// Update ticket price to 200 and keep it updated for 3 days.
$ticket->postpone()
    ->keepForDays(3)
    ->update(['price' => 200]);

使用 delay for

使用 delay for,更新将在指定数量的分钟、小时或天后执行。

$ticket = Ticket::find(1);

// Update ticket price to 200 after 60 minutes from now and keep it like that for unlimited time.
$ticket->postpone()
    ->delayForMinutes(60)
    ->update(['price' => 200]);

// Update ticket price to 200 after 12 hours from now and keep it like that for unlimited time.
$ticket->postpone()
    ->delayForHours(12)
    ->update(['price' => 200]);

// Update ticket price to 200 after 3 days from now and keep it like that for unlimited time.
$ticket->postpone()
    ->delayForDays(3)
    ->update(['price' => 200]);

使用时间戳

您还可以使用时间戳来指定更新某些模型的确切时间。

$product = Product::find(1);

// Update product to be unavailable from 1st January.
$product->postpone()
    ->startFrom("2023-01-01 00:00:00")
    ->update(['is_available' => false]);

// Update product to be unavailable until 4th January.
$product->postpone()
    ->revertAt("2023-04-01 00:00:00")
    ->update(['is_available' => false]);

// Update product to be unavailable from 1st January to 4th January.
$product->postpone()
    ->startFrom("2023-01-01 00:00:00")
    ->revertAt("2023-04-01 00:00:00")
    ->update(['is_available' => false]);

使用组合

也可以将特定的分钟、小时或天数与时间戳结合使用。

$product = Product::find(1);

// Update product to be unavailable from 1st January and keep that state for 1 day.
$product->postpone()
    ->startFrom("2023-01-01 00:00:00")
    ->keepForDays(1)
    ->update(['is_available' => false]);

// Update product to became unavailable after 60 minutes from now and keep that state until 4th January.
$product->postpone()
    ->delayForMinutes(60)
    ->revertAt("2023-04-01 00:00:00")
    ->update(['price' => 200]);

在模型上使用方法

默认情况下,所有可填充属性都被允许延迟,但您可以通过重写 allowedPendingAttributes 方法来更改这一点。

use Illuminate\Database\Eloquent\Model;
use Stfn\PendingUpdates\Models\Concerns\HasPendingUpdates;

class Ticket extends Model
{
    use HasPendingUpdates;
    
    public function allowedPendingAttributes()
    {
        return ['price'];
    }
}

请注意,这些字段也需要是可填充的。

有时计划中的更新可能会失败,原因多种多样。默认情况下,该包会将异常发送到您配置的外部报告服务,例如 Sentry、Flare 或 Bugsnag,然后从 pending_updates 表中删除记录。如果您想更改此行为,您可以在 PendingUpdateModel 上重写 updateCannotBeApplied 方法。

use Illuminate\Database\Eloquent\Model;
use Stfn\PendingUpdates\Models\PendingUpdate;

class CustomPendingUpdate extends PendingUpdate
{    
    public function updateCannotBeApplied($exception, $model)
    {
        // Your custom logic here
    }
}

测试

composer test

更新日志

有关最近更改的更多信息,请参阅 更新日志

鸣谢

许可协议

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