lab2view / laravel-model-archive
...
Installs: 1 129
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 0
Open Issues: 0
Requires
- php: ^8.1|^8.2|^8.3
- illuminate/console: ^9.0|^10.9|^11.0
- illuminate/support: ^9.0|^10.9|^11.0
- larastan/larastan: ^2.7
- laravel/pint: ^1.8
- phpstan/phpstan: ^1.10
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.8
- orchestra/testbench: ^9.3
- phpstan/phpstan: ^1.11
- dev-main
- dev-feat/php_stan
- dev-feat/find_last_release_v2_commit
- dev-feature/php_stan_tests
- dev-feature/udpate_config_file
- dev-feature/archive_v2
- dev-feat/empty_propery_archive_with
- dev-feature/check_force_delete
- dev-feature/ceck_force_delete
- dev-feature/real_deletion
- dev-feature/test
- dev-feat/ignore_unvalidated_when_reading_archive_database
- dev-feat/archivable
- dev-feat/query_unarchived
- dev-feat/between_script
- dev-feat/update_readme
- dev-feature/overide_archive_connection
- dev-feat/overide_archive_connection
- dev-feature/initializing_archiving
- dev-develop
This package is auto-updated.
Last update: 2024-12-06 14:32:43 UTC
README
A simple package for making Laravel Eloquent models 'archivable'. This package allows for the easy archiving of models in a secondary database and provides the necessary macros to retrieve them when needed.
Installation
You can install the package via composer:
composer require lab2view/laravel-model-archive
Usage
Migrations
Archiving is done in 2 steps. The first consists of copying the models to the database dedicated to archiving and the second step is to check that the archiving of said models has been done well and to delete them from the main database. This package to work must create a polymorphic table intended to record the archived archivable models
Schema::create('archives', function (Blueprint $table) { $table->id(); $table->string("archive_with"); $table->morphs("archivable"); $table->timestamp('validated_at')->nullable(); $table->timestamps(); });
To publish migration file use
php artisan vendor:publish --provider="Lab2view\ModelArchive\ModelArchiveServiceProvider" --tag="migrations"
Commands
This package provides 2 commands to respectively copy the archivable models to the database dedicated to archiving and a second one to validate the archiving and delete the archived elements from the main database.
lab2view:model_archive
lab2view:validate_model_archive
This commands require direct manipulation with the database by executing queries on the model defined as archiveable. For example, using the LadaCacheTrait
trait of spiritix/lada-cache package implies that queries on models do not always return the Illuminate\Database\Eloquent\Builder
interface but a builder version specific to this package based on the cache. In this case, it is necessary to execute the lada-cache:desable
command before the commands and lada-cache:enable
after.
Between Commands
It is possible to register callables that will be called before or after commands or a specific command by registering them in the between_commands
config. Example:
class BeforeAndAfterAnything { public function __invoke() { // Do Anything after and before commands } } class BeforeAnything { public function __invoke() { Artisan::command("lada-cache:desable", ...); } } class AfterAnything { public function __invoke() { Artisan::command("lada-cache:enable", ...); } } function afterModelArchive(){ // Do anything after the lab2view:model_archive command has been successfully executed } return [ ..., 'between_commands' => [ 'all' => [new BeforeAndAfterAnything], 'before' => [ 'all' => [new BeforeAnything], 'lab2view:model_archive' => ['afterModelArchive'], 'lab2view:validate_model_archive' => [] ], 'after' => [ 'all' => [], 'lab2view:model_archive' => [], 'lab2view:validate_model_archive' => [] ] ] ];
Config
Archiving needs to know the database to dedicate to archiving [archve_db_connection] and the main database from which to clone archiveable models [main_db_connection].
[
'main_db_connection' => env('DB_CONNECTION', 'mysql'),
'archive_db_connection' => env('ARCHIVE_DB_CONNECTION', 'archive'),
'archive_delete_from_main' => env('ARCHIVE_DELETE_FROM_MAIN', true),
]
To publish config file use:
php artisan vendor:publish --provider="Lab2view\ModelArchive\ModelArchiveServiceProvider" --tag="config"
Eloquent
You can now, safely, include the Archivable
trait in your Eloquent model:
namespace App\Models; use \Illuminate\Database\Eloquent\Model; use Lab2view\ModelArchive\Traits\Archivable; class Sale extends Model { use Archivable; ... }
Extensions
The extensions shipped with this trait include; Archivable
, Archived
, Unarchived
, OnlyArchived
and can be used accordingly:
$onlyArchivedSales = Sale::select('*')->onlyArchived();
By default, the global scope of this trait uses the unarchived
extension when the trait Archivable
is added to a model. This prevents manipulation of archived models whose validation has not yet been done by the second command and which still exist in the source database.
License
The MIT License (MIT). Please see License File for more information.