dixieio/eloquent-model-future

为 Eloquent 模型安排更改

v0.4.0 2018-02-22 09:21 UTC

This package is not auto-updated.

Last update: 2024-09-20 20:00:57 UTC


README

为您的模型提供一个美好且可预测的未来

一个允许您以简单方式规划模型更改的包。

安装

通过 composer 需求此包

composer require dixie/eloquent-model-future

运行包迁移以创建一个 futures 表,该表将存储所选模型的所有未来。

php artisan migrate

安排命令以持久化未来计划

$scheduler->command('future:schedule')->daily();

用法

在您希望使用的模型上使用 HasFuture 特性。

class User extends Model
{
    use Dixie\EloquentModelFuture\HasFuture;
}

现在您可以规划、编辑和移除未来计划中的属性更改。

这是您如何与模型未来交互的方式。

$user = User::find(1);
$nextMonth = Carbon\Carbon::now()->addMonth();

// Plan a profile change for new years eve
$user->future()->plan([
    'bio' => 'Happy developer time. Wooh!',
    'mood' => 'excited',
])->for($nextMonth);

// Does our user have any scheduled plans for next month?
$user->future()->anyPlansFor($nextMonth); // true

// How does our user look in the future
$user->future()->see($newYearsEve);
User {
  'attributes': {
      'id': 1,
      'name': 'John Doe',
      'bio': 'Happy developer time. Wooh!',
      'mood': 'excited',
      [...]
  }
}

// You can commit to the changes by future plans after you've seen them
// ... this will fill the `committed` column with todays date
$user->future()->see($newYearsEve)->commit(); // true

// Access all future plans for the given day
$futurePlans = $user->future()->getPlansFor(Carbon $date)
FutureCollection {
    Future {
        'attributes' => [
            'bio' => 'Happy [...]',
            'mood' => 'excited',
        ]
    }
}

// There are some helper methods that come with the FutureCollection
$futurePlans->original();   // Original user state. Returns a user instance.

$futurePlans->result();     // How the user will look when collection is applied to user.
$futurePlans->resultDiff()  // Shows which attributes has changed 
                            // and what the values would be before and after

API 参考