承接 makidizajnerica/laravel-snapshoter 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

makidizajnerica/laravel-snapshoter

最新稳定版本:v4.0

Composer 安装命令:

composer require makidizajnerica/laravel-snapshoter

包简介

Laravel model snapshoter.

README 文档

README

Laravel model snapshoter. Make snapshots for the models, revert model state to previous snapshot.

Installation

composer require makidizajnerica/laravel-snapshoter

Publish migrations using:

php artisan vendor:publish --tag=snapshoter-migrations

After that be sure to run migrations to generate snapshots table:

php artisan migrate

Then publish config file snapshoter.php using:

php artisan vendor:publish --tag=snapshoter-config

Usage

Your model needs to implement MakiDizajnerica\Snapshoter\Contracts\Snapshotable and also to use MakiDizajnerica\Snapshoter\HasSnapshots trait. Next define snapshotFields method that will return model attributes you want to save to the snapshots:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use MakiDizajnerica\Snapshoter\HasSnapshots;
use MakiDizajnerica\Snapshoter\Contracts\Snapshotable as SnapshotableContract;

class Project extends Model implements SnapshotableContract
{
    use HasSnapshots;

    /**
     * Get attributes for the snapshot.
     *
     * @return array<string, mixed>
     */
    public function snapshotAttributes(): array
    {
        return $this->only([
            'name',
            'description',
            'start_at',
            'end_at',
        ]);
    }

    // ...
}

You will then have some methods available on your model:

<?php

use App\Models\Project;
use MakiDizajnerica\Snapshoter\Models\Snapshot;

// Make snapshot for the model
$project = Project::first();
$project->makeSnapshot();
// or
snapshoter($project);

// Revert model's state to previous snapshot
$project->revertToPreviousSnapshot();

// Revert model's state to the snapshot from a few steps back
$project->revertToPreviousSnapshot(3);

// You can also pass Snapshot instance
$snapshot = $project->snapshots()->first();
$project->revertToSnapshot($snapshot);

If you want to create, update or save your model with the snapshot, you can use:

<?php

use App\Models\Project;

// Create model with initial snapshot
$project = Project::createWithSnapshot([/* ... */]);

// Update model and make snapshot
$project->updateWithSnapshot([/* ... */]);

// Save model and make snapshot
$project->fill([/* ... */])->saveWithSnapshot();

Another option would be to use observer and automatically make snapshot on model create or update:

<?php

namespace App\Observers;

use App\Models\Project;

class ProjectObserver
{
    /**
     * Handle the Project "created" event.
     *
     * @param  \App\Models\Project $project
     * @return void
     */
    public function created(Project $project)
    {
        $project->makeSnapshot();
    }

    /**
     * Handle the Project "updated" event.
     *
     * @param  \App\Models\Project $project
     * @return void
     */
    public function updated(Project $project)
    {
        $project->makeSnapshot();
    }
}

Author

Nemanja Marijanovic (n.marijanovic@hotmail.com)

Licence

Copyright © 2021, Nemanja Marijanovic n.marijanovic@hotmail.com

All rights reserved.

For the full copyright and license information, please view the LICENSE file that was distributed within the source root of this package.

统计信息

  • 总下载量: 15
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-07-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固