1. Go to this page and download the library: Download gazugafan/laravel-temporal library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gazugafan / laravel-temporal example snippets
//create your table like normal...
Schema::create('widgets', function ($table) {
$table->increments('id');
$table->timestamps();
$table->string('name');
});
//add the columns and keys needed for the temporal features...
Gazugafan\Temporal\Migration::make_temporal('widgets');
class Widget extends Model
{
use Temporal; //add all the temporal features
protected $dates = ['temporal_start', 'temporal_end']; //if you want these auto-cast to Carbon, go for it!
}
class Widget extends Model
{
use Temporal; //add all the temporal features
protected $version_column = 'version';
protected $temporal_start_column = 'temporal_start';
protected $temporal_end_column = 'temporal_end';
protected $temporal_max = '2999-01-01 00:00:00';
protected $overwritable = ['worthless_column']; //columns that can simply be overwritten if only they are being updated
}
$widget = new Widget();
$widget->name = 'Cawg';
$widget->save(); //inserts a new widget at version 1 with temporal_start=now and temporal_end='2999-01-01'
$widget->name = 'Cog';
$widget->save(); //ends the lifespan of version 1 by updating its temporal_end=now, and inserts a new revision with version=2
$anotherWidget = Widget::create(['name'=>'Other Cog']); //another way to insert a new widget at version 1
$widget123 = Widget::find(123); //gets the current version of widget #123.
$blueWidgets = Widget::where('color', 'blue')->get(); //gets the current version of all the blue widgets
$widget123s = Widget::where('id', 123)->allVersions()->get(); //gets all versions of widget #123
$blueWidgets = Widget::allVersions()->where('color', 'blue')->get(); //gets all versions of all blue widgets
$widget = Widget::find(123); //gets the current version of widget #123, like normal
$firstWidget = $widget->firstVersion(); //gets the first version of widget #123
$secondWidget = $firstWidget->nextVersion(); //gets version 2 of widget #123
$fifthWidget = $widget->atVersion(5); //gets version 5 of widget #123
$fourthWidget = $fifthWidget->previousVersion(); //gets version 4 of widget #123
$latestWidget = $widget->latestVersion(); //gets the latest version of widget #123 (not necessarily the current version if it was deleted)
$yesterdaysWidget = $widget->atDate(Carbon::now()->subDays(1)); //get widget #123 as it existed at this time yesterday
$januaryWidgets = $widget->inRange('2017-01-01', '2017-02-01'); //get all versions of widget #123 that were active at some point in Jan. 2017
$firstWidgets = Widget::where('id', 123)->firstVersions()->get(); //gets the first version of widget #123 (in a collection with 1 element)
$firstWidget = $firstWidgets->first(); //since this is a collection, you can use first() to get the first (and in this case only) element
$secondBlueWidgets = Widget::versionsAt(2)->where('color', 'blue')->get(); //gets the second version of all blue widgets
$noonWidgets = Widget::versionsAtDate('2017-01-01 12:00:00')->get(); //gets all widgets as they were at noon on Jan. 1st 2017
//get all versions of widgets that were red and active at some point last week...
$lastWeeksRedWidgets = Widget::where('color', 'red')->versionsInRange(Carbon::now()->subWeeks(2), Carbon::now()->subWeeks(1))->get();
$widget = Widget::create(['name'=>'cog']); //create a new widget like normal
$widgetID = $widget->id; //get the widget's ID
$widget->delete(); //nothing is really DELETEd from the database. We just update temporal_end to now
$deletedWidget = Widget::find($widgetID); //returns null because the record no longer has a current version
$widget->restore(); //would restore the deleted record from the example above
$widget->purge(); //that widget's gone for good... like it never even existed in the first place.
//don't even try this unless you want to totally screw
//things up (or you really know what you're doing)...
App\Widget::where('active', 1)->update(['description'=>'An active widget']);
$copyA = Widget::find(1);
$copyB = Widget::find(1);
$copyA->name = 'new name A';
$copyA->save(); //updates the original revision and inserts a new revision
$copyB->name = 'new name B';
$copyB->save(); //attempts to update the original revision again and throws an error
public function validateTemporalUnique($attribute, $value, $parameters)
{
$this->parameter position holds the name of the column that needs to
// be verified as unique. If this parameter isn't specified we will just
// assume that this column to be verified shares the attribute's name.
$column = $this->getQueryColumn($parameters, $attribute);
list($idColumn, $id) = [null, null];
if (isset($parameters[2])) {
list($idColumn, $id) = $this->getUniqueIds($parameters);
}
// The presence verifier is responsible for counting rows within this store
// mechanism which might be a relational database or any other permanent
// data store like Redis, etc. We will use it to determine uniqueness.
$verifier = $this->getPresenceVerifierFor($connection);
$extra = $this->getUniqueExtra($parameters);
if ($this->currentRule instanceof Unique) {
$extra = array_merge($extra, $this->currentRule->queryCallbacks());
}
//add the default temporal property...
if (!array_key_exists('temporal_end', $extra))
$extra['temporal_end'] = '2999-01-01';
return $verifier->getCount(
$table, $column, $value, $id, $idColumn, $extra
) == 0;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.