dixieio/eloquent-model-future

Schedule changes for Eloquent models

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

This package is not auto-updated.

Last update: 2024-11-15 20:43:39 UTC


README

Give your models a nice and predictable future

A package that lets you plan changes to your models in a simple manner.

Installation

Require the package via composer

composer require dixie/eloquent-model-future

Run the package migrations to create a futures table, that will hold every future of your selected models.

php artisan migrate

Schedule the command to persist future plans

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

Usage

On your desired models use the HasFuture trait.

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

Now you can plan out, edit and take away attribute changes, planned for the future.

Here is how you interact with your models future.

$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 Reference