Download the PHP package aaronheath/laravel-model-copy without Composer
On this page you can find all versions of the php package aaronheath/laravel-model-copy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download aaronheath/laravel-model-copy
More information about aaronheath/laravel-model-copy
Files in aaronheath/laravel-model-copy
Package laravel-model-copy
Short Description Copy one Laravel Eloquent model from one table to another.
License MIT
Homepage https://github.com/aaronheath/laravel-model-copy
Informations about the package laravel-model-copy
Laravel Model Copy
Laravel model copy helps you copy, move records from one database table to another. This solution is designed for moving un-needed records to another table in a safe manner which can then be backed-up and, if need, truncated.
The package is also able to assist in performing batch model deletions over using the same conditions as are made available when copying models.
Installation
You can install the package via composer:
Usage
The package can be used in one of three ways:
- A) Copy or move an individual model.
- B) Batch copy or move many models from a query.
- C) Batch deletion of models from a query.
Copy or move individual model
Copying an individual model is as easy as...
Copying an individual model and deleting the original record is a easy as including ->deleteOriginal()
.
We may not want to copy or move the model if it's after a certain time. This can be achieved by including ->processBefore(now()->addHour())
Batch copy or move many models from a query
Most likely you won't be wanting to move just one record, this is where batch copying or moving comes into play.
Here's a simple batch copy which will make a copy of all model ExampleA records where they were handled_at
more than three years ago.
If we want to instead delete the original model, all we need to do is include ->deleteOriginal()
.
If we want limit on how many records we want to copy / move in one go, we can use ->limit(1000)
.
By default queries will be chunked into 100 record batches. If you wish to use your own chunking value, this can be achieved by ->chunk(500)
.
To use a custom (not 'id') column to chunk by use ->chunkColumn()
.
Up until now, all actions happen in one syncronious stream. In real world situation it's better to process individual copy / move model actions by pushing them to the queue. This can be achieved by including ->copyModelsAsJobs()
.
If a queue besides the default wants to be used, then include ->onQueue('queue-name')
.
When moving large sets of data it may take quite some time. In these cases you may want to group your moving batches into blocks of time. Let's say we want to run the script nightly at 23:00 and want to make sure we stop moving copying models at 05:00 the next day. This can be achieved by using ->processBefore(now()->addDay()->setTime(5, 0, 0)
.
We may also want to limit how many records are copied / moved in any given minute. To do this a rate per minute (rpm) can be defined. If we're wanting to ensure that we only copy 20 records per minute then we can use ->rpm(20)
. Using this feature requires ->copyModelsAsJobs()
to be used.
Real world example
Let's say we want to move all records that were handled_at
over three years ago to another table. We want to only have the run until 06:00 the next day. We also want to limit load on the database, so we'll only move 100 records per minute. To achieve this we'd use the following...
Delete individual model
Deleting an individual model is as easy as...
Batch deleting models from a query
Most likely you won't be wanting to delete just one record, this is where batch deleting comes into play.
Here's a simple batch delete which will delete all model ExampleA records where they were handled_at
more than three years ago.
If we want limit on how many records we want to delete in one go, we can use ->limit(1000)
.
By default queries will be chunked into 100 record batches. If you wish to use your own chunking value, this can be achieved by ->chunk(500)
.
To use a custom (not 'id') column to chunk by use ->chunkColumn()
.
Up until now, all actions happen in one syncronious stream. In real world situation it's better to process individual model deletion actions by pushing them to the queue. This can be achieved by including ->copyModelsAsJobs()
.
If a queue besides the default wants to be used, then include ->onQueue('queue-name')
.
When deleting large sets of data it may take quite some time. In these cases you may want to group your deleting batches into blocks of time. Let's say we want to run the script nightly at 23:00 and want to make sure we stop moving deleting models at 05:00 the next day. This can be achieved by using ->processBefore(now()->addDay()->setTime(5, 0, 0)
.
We may also want to limit how many records are deleted in any given minute. To do this a rate per minute (rpm) can be defined. If we're wanting to ensure that we only delete 20 records per minute then we can use ->rpm(20)
. Using this feature requires ->copyModelsAsJobs()
to be used.
Testing
Credits
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-model-copy with dependencies
illuminate/support Version ^8.61|^9.0
aaronheath/class-logger Version ^1.0