PHPackages                             elipzis/laravel-pastable-model - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. elipzis/laravel-pastable-model

ActiveLibrary[Database &amp; ORM](/categories/database)

elipzis/laravel-pastable-model
==============================

Cut/Copy &amp; Paste Laravel Eloquent model data into another table

v0.2.0(2y ago)11791[4 PRs](https://github.com/elipZis/laravel-pastable-model/pulls)MITPHPPHP ^8.2CI passing

Since Aug 31Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/elipZis/laravel-pastable-model)[ Packagist](https://packagist.org/packages/elipzis/laravel-pastable-model)[ Docs](https://github.com/elipzis/laravel-pastable-model)[ RSS](/packages/elipzis-laravel-pastable-model/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (13)Versions (7)Used By (0)

Cut/Copy &amp; Paste Laravel Eloquent models into another table
===============================================================

[](#cutcopy--paste-laravel-eloquent-models-into-another-table)

[![Latest Version on Packagist](https://camo.githubusercontent.com/47d68e00e7a67da9aab489dcb3425fba80aa60325b72f2e2973dd7454460c2fb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f656c69707a69732f6c61726176656c2d7061737461626c652d6d6f64656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/elipzis/laravel-pastable-model)[![GitHub Tests Action Status](https://camo.githubusercontent.com/7330d282f8bc8daac2a059858dce6d652a103f3d47306b844781aafc92154528/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f656c69707a69732f6c61726176656c2d7061737461626c652d6d6f64656c2f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/elipzis/laravel-pastable-model/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/3c249721dc6c9dc7f1cb67158c82616b9b43c7a157aab9fcb856cbce11e24043/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f656c69707a69732f6c61726176656c2d7061737461626c652d6d6f64656c2f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/elipzis/laravel-pastable-model/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/8e3e4ce2104318d14446864ebf96abcf64e5a629e1234e731fc359613992b616/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f656c69707a69732f6c61726176656c2d7061737461626c652d6d6f64656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/elipzis/laravel-pastable-model)

Enable your models to regularly cut/copy &amp; paste their data into another table.

- Cut &amp; Paste or Copy &amp; Paste
- Scheduled Jobs available to regularly &amp; asynchronously run
- Cut &amp; Paste in chunks, to split potential long-running processes
- Store data e.g. into logging or daily tables and keep the production data clean

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require elipzis/laravel-pastable-model
```

You can publish the config file with:

```
php artisan vendor:publish --tag="pastable-model-config"
```

This is the contents of the published config file:

```
return [
    //The default cut&paste chunk size (limit)
    'chunkSize' => 1000,
    //Auto-create tables, if not existing
    'autoCreate' => false,
    //Enable detailed logging to any accepted and configured level
    'logging' => [
        'enabled' => false,
        'level' => null,
    ],
];
```

Usage
-----

[](#usage)

To make your model copy &amp; pastable, add the trait `CopyPastable`. It will copy &amp; paste your configured query result into the target table.

```
...
use ElipZis\Pastable\Models\Traits\CopyPastable;
...

class YourModel extends Model {

    use CopyPastable;
    ...
```

To make your model cut &amp; pastable, add the trait `CutPastable`. It will cut (delete) &amp; paste your configured query result into the target table. If more rows than the chunk size (limit) are affected, it will respawn its own job until completed.

```
...
use ElipZis\Pastable\Models\Traits\CutPastable;
...

class YourModel extends Model {

    use CutPastable;
    ...
```

### Configuration

[](#configuration)

To use any trait, you need to configure two settings:

- the target table
- the query to read its data from

#### Target table *(mandatory)*

[](#target-table-mandatory)

You must define the target table name.

```
...

class YourModel extends Model {

    ...
    protected string $pastableTable = 'log_something';
    ...
```

or by overriding the getter function, to e.g. create dynamic table names

```
...

class YourModel extends Model {

    ...
    public function getPastableTable(): string
    {
        return 'log_something_' . Carbon::now()->format('Y_m_d');
    }
    ...
```

If the table does not exist, you can use the configuration setting `autoCreate` and set it to `true` to have the system try to create the table from your query source.

**It is recommended for you to create the table manually or via migration, as the automation is not fully tested and functional to any database system and table structure.**

#### Query *(mandatory)*

[](#query-mandatory)

You must define the query to use to read data and cut/copy &amp; paste into the target table.

```
...

class YourModel extends Model {

    ...
    public function getPastableQuery(): Builder
    {
        return static::query()->where('created_at', '
