{"id":20806490,"url":"https://github.com/elipzis/laravel-pastable-model","last_synced_at":"2025-05-07T04:25:12.096Z","repository":{"id":191742327,"uuid":"685017553","full_name":"elipZis/laravel-pastable-model","owner":"elipZis","description":"⚙️ Cut/Copy \u0026 Paste Laravel Eloquent model data into another table","archived":false,"fork":false,"pushed_at":"2024-07-08T07:11:31.000Z","size":70,"stargazers_count":13,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-24T19:53:06.339Z","etag":null,"topics":["copy-paste","cut-paste","eloquent","entity","laravel","logging","model"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/elipZis.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-30T10:37:17.000Z","updated_at":"2024-07-16T13:59:11.000Z","dependencies_parsed_at":null,"dependency_job_id":"9f616846-9085-4eec-ae71-de347bb8dd4e","html_url":"https://github.com/elipZis/laravel-pastable-model","commit_stats":null,"previous_names":["elipzis/laravel-pastable-model"],"tags_count":2,"template":false,"template_full_name":"spatie/package-skeleton-laravel","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elipZis%2Flaravel-pastable-model","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elipZis%2Flaravel-pastable-model/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elipZis%2Flaravel-pastable-model/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elipZis%2Flaravel-pastable-model/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/elipZis","download_url":"https://codeload.github.com/elipZis/laravel-pastable-model/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252811709,"owners_count":21807997,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["copy-paste","cut-paste","eloquent","entity","laravel","logging","model"],"created_at":"2024-11-17T19:20:21.194Z","updated_at":"2025-05-07T04:25:12.071Z","avatar_url":"https://github.com/elipZis.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cut/Copy \u0026 Paste Laravel Eloquent models into another table\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/elipzis/laravel-pastable-model.svg?style=flat-square)](https://packagist.org/packages/elipzis/laravel-pastable-model)\n[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/elipzis/laravel-pastable-model/run-tests.yml?branch=main\u0026label=tests\u0026style=flat-square)](https://github.com/elipzis/laravel-pastable-model/actions?query=workflow%3Arun-tests+branch%3Amain)\n[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/elipzis/laravel-pastable-model/fix-php-code-style-issues.yml?branch=main\u0026label=code%20style\u0026style=flat-square)](https://github.com/elipzis/laravel-pastable-model/actions?query=workflow%3A\"Fix+PHP+code+style+issues\"+branch%3Amain)\n[![Total Downloads](https://img.shields.io/packagist/dt/elipzis/laravel-pastable-model.svg?style=flat-square)](https://packagist.org/packages/elipzis/laravel-pastable-model)\n\nEnable your models to regularly cut/copy \u0026 paste their data into another table.\n\n- Cut \u0026 Paste or Copy \u0026 Paste\n- Scheduled Jobs available to regularly \u0026 asynchronously run\n- Cut \u0026 Paste in chunks, to split potential long-running processes\n- Store data e.g. into logging or daily tables and keep the production data clean\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require elipzis/laravel-pastable-model\n```\n\nYou can publish the config file with:\n\n```bash\nphp artisan vendor:publish --tag=\"pastable-model-config\"\n```\n\nThis is the contents of the published config file:\n\n```php\nreturn [\n    //The default cut\u0026paste chunk size (limit)\n    'chunkSize' =\u003e 1000,\n    //Auto-create tables, if not existing\n    'autoCreate' =\u003e false,\n    //Enable detailed logging to any accepted and configured level\n    'logging' =\u003e [\n        'enabled' =\u003e false,\n        'level' =\u003e null,\n    ],\n];\n```\n\n## Usage\n\nTo make your model copy \u0026 pastable, add the trait `CopyPastable`.\nIt will copy \u0026 paste your configured query result into the target table.\n\n```php\n...\nuse ElipZis\\Pastable\\Models\\Traits\\CopyPastable;\n...\n\nclass YourModel extends Model {\n\n    use CopyPastable;    \n    ...\n```\n\nTo make your model cut \u0026 pastable, add the trait `CutPastable`.\nIt will cut (delete) \u0026 paste your configured query result into the target table.\nIf more rows than the chunk size (limit) are affected, it will respawn its own job until completed.\n\n```php\n...\nuse ElipZis\\Pastable\\Models\\Traits\\CutPastable;\n...\n\nclass YourModel extends Model {\n\n    use CutPastable;    \n    ...\n```\n\n### Configuration\n\nTo use any trait, you need to configure two settings:\n\n- the target table\n- the query to read its data from\n\n#### Target table _(mandatory)_\n\nYou must define the target table name.\n\n```php\n...\n\nclass YourModel extends Model {\n\n    ...\n    protected string $pastableTable = 'log_something';    \n    ...\n```\n\nor by overriding the getter function, to e.g. create dynamic table names\n\n```php\n...\n\nclass YourModel extends Model {\n\n    ...\n    public function getPastableTable(): string\n    {\n        return 'log_something_' . Carbon::now()-\u003eformat('Y_m_d');\n    }\n    ...\n```\n\nIf the table does not exist, you can use the configuration setting `autoCreate` and set it to `true` to have the system\ntry to create the table from your query source.\n\n**It is recommended for you to create the table manually or via migration, as the automation is not fully tested and\nfunctional to any database system and table structure.**\n\n#### Query _(mandatory)_\n\nYou must define the query to use to read data and cut/copy \u0026 paste into the target table.\n\n```php\n...\n\nclass YourModel extends Model {\n\n    ...\n    public function getPastableQuery(): Builder\n    {\n        return static::query()-\u003ewhere('created_at', '\u003c=', now()-\u003esubDay());\n    }\n    ...\n```\n\nYou can use any query that returns a `Builder` object.\n\nIn the case of cut \u0026 paste, the default `chunkSize` is used as a limiter. You can set your own limit by\nadding `-\u003elimit()` to the query or override the configuration setting in general.\n\n#### Connection _(optional)_\n\nYou can give a separate connection if you want the target table to be generated and filled in e.g. another database.\n\n```php\n...\n\nclass YourModel extends Model {\n\n    ...\n    protected string $pastableConnection = 'logging';    \n    ...\n```\n\n### Run\n\nAfter implementation and configuration, you got three options to trigger the cut/copy \u0026 paste jobs:\n\n- Manually dispatching the jobs\n- Scheduled dispatch\n- Running a command to trigger it manually\n\n#### Scheduled\n\nThe preferred way is to run the job on a schedule, configured via the Kernel, e.g. daily:\n\n```php\nnamespace App\\Console;\n\n...\nuse ElipZis\\Pastable\\Jobs\\PastableJob;\n...\n\nclass Kernel extends ConsoleKernel\n\n    ...\n    protected function schedule(Schedule $schedule)\n    {\n        ...\n        $schedule-\u003ejob(PastableJob::class)-\u003edaily();\n        ...\n    }\n    ...\n```\n\n#### Via Command\n\nYou may also trigger the execution manually by using the command(s):\n\n- All cut/copy \u0026 pastable model classes: `php artisan pastable:all`\n- Only copy \u0026 pastable model classes: `php artisan pastable:copy`\n- Only cut \u0026 pastable model classes: `php artisan pastable:cut`\n\n#### Manual dispatch\n\nThe final option is to trigger the job manually inside any of your functions, any logic, any application code:\n\n```php\n...\nuse ElipZis\\Pastable\\Jobs\\PastableJob;\n...\n\nclass YourClass\n\n    ...\n    protected function yourFunction()\n    {\n        ...\n        PastableJob::dispatch();\n        ...\n    }\n    ...\n```\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Notes\n\nThis package is heavily inspired by two incredible resources:\n\n- [Laravel Prunable](https://laravel.com/docs/10.x/eloquent#pruning-models)\n- [Flare's cleaning big tables](https://flareapp.io/blog/7-how-to-safely-delete-records-in-massive-tables-on-aws-using-laravel)\n\nKudos and Thanks to both for the inspiration.\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](.github/SECURITY.md) on how to report security vulnerabilities.\n\n## Credits\n\n- [elipZis GmbH](https://github.com/elipZis)\n- [NeA](https://github.com/nea)\n- [All Contributors](https://github.com/elipZis/laravel-pastable-model/contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felipzis%2Flaravel-pastable-model","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felipzis%2Flaravel-pastable-model","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felipzis%2Flaravel-pastable-model/lists"}