PHP code example of plank / snapshots

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

    

plank / snapshots example snippets




namespace App\Http\Middleware;

use Closure;
use Plank\Snapshots\Contracts\ManagesVersions;

class SetActiveVersion
{
    public function __construct(
        protected ManagesVersions $versions
    ) {
    }

    public function handle($request, Closure $next)
    {
        $version = $request->route('version');

        if ($version = $this->versions->byNumber($version)) {
            $this->versions->setActive($version);
        }

        return $next($request);
    }
}



use Plank\Snapshots\Facades\SnapshotSchema;
use Plank\Snapshots\Migrations\SnapshotBlueprint;
use Plank\Snapshots\Migrations\SnapshotMigration;

return new class extends SnapshotMigration
{
    public function up()
    {
        $this->schema->create('blocks', function (SnapshotBlueprint $table) {
            $table->id();
            $table->unsignedBigInteger('page_id');
            $table->string('name');
            $table->morphs('blockable');
            $table->timestamps();

            $table->foreign('page_id')->references('id')->onSnapshot('pages');
        });
    }

    public function down()
    {
        SnapshotSchema::dropIfExists('blocks');
    }
}



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Plank\Snapshots\Concerns\AsVersionedContent;
use Plank\Snapshots\Contracts\Versioned;

class Page extends Model implements Versioned
{
    use AsVersionedContent;
}



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Plank\Snapshots\Concerns\InteractsWithVersionedContent;

class User extends Model
{
    use InteractsWithVersionedContent;

    public function pages()
    {
        return $this->belongsToMany(Page::class);
    }
}
bash
php artisan snapshots:install
bash
php artisan vendor:publish --provider="Plank\Snapshots\SnapshotsServiceProvider" --tag="config"
bash
php artisan migrate

INFO  Running migrations.

v1_0_0_2023_09_25_000000_create_pages_table    ............................... 10ms DONE