PHP code example of hoangphamdev / laravel-bitemporal

1. Go to this page and download the library: Download hoangphamdev/laravel-bitemporal 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/ */

    

hoangphamdev / laravel-bitemporal example snippets


use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->timestamps();
    $table->bitemporal();
});

Schema::table('posts', function (Blueprint $table) {
    $table->dropBitemporal();
});

use Illuminate\Database\Eloquent\Model;
use HoangPhamDev\Bitemporal\Traits\HasBitemporal;

class Post extends Model
{
    use HasBitemporal;
}

Post::getBitemporalColumns();
Post::getInfinityDatetime();

$posts = Post::all();

$posts = Post::current()->get();

$posts = Post::query()->current()->get();

$posts = Post::asOf('2025-01-01 00:00:00')->get();

$query = Post::withoutBitemporal()->history();

$posts = Post::withoutBitemporal()->get();

$posts = Post::withoutGlobalScope('bitemporal_current')->get();

Post::find(1);
Post::findMany([1, 2]);
Post::findOrFail(1);
Post::findOrNew(1);
Post::findOr(1, fn () => null);
Post::findSole(1);

$post = Post::find(1);

$post->bitemporalUpdate([
    'title' => 'Published title',
]);

$post->bitemporalUpdate([
    'title' => 'Published title',
], '2025-01-08 00:00:00');

$post = Post::find(1);

$post->bitemporalDelete();

$post->bitemporalDelete('2025-01-08 00:00:00');

$post = Post::find(1);

$post->delete();

Post::withoutBitemporal()->whereIn('id', [1, 2])->delete();
Post::destroy([1, 2]);
Post::withoutBitemporal()->whereIn('id', [1, 2])->toBase()->delete();

use HoangPhamDev\Bitemporal\Support\BitemporalDefaults;

$value = BitemporalDefaults::INFINITY_DATETIME;

use HoangPhamDev\Bitemporal\Traits\HasBitemporal;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasBitemporal;

    protected $fillable = [
        'title',
        'valid_from',
        'valid_to',
        'transaction_from',
        'transaction_to',
    ];

    protected $casts = [
        'operated_at' => 'datetime',
        'valid_from' => 'datetime',
        'valid_to' => 'datetime',
        'transaction_from' => 'datetime',
        'transaction_to' => 'datetime',
    ];
}