PHPackages                             georgehanson/laravel-persisters - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. georgehanson/laravel-persisters

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

georgehanson/laravel-persisters
===============================

A laravel package for creating persisters

v1.1(7y ago)2431MITPHP

Since Oct 17Pushed 7y agoCompare

[ Source](https://github.com/GeorgeHanson/laravel-persisters)[ Packagist](https://packagist.org/packages/georgehanson/laravel-persisters)[ RSS](/packages/georgehanson-laravel-persisters/feed)WikiDiscussions master Synced yesterday

READMEChangelog (3)Dependencies (6)Versions (4)Used By (0)

Laravel Persisters
==================

[](#laravel-persisters)

[![Build Status](https://camo.githubusercontent.com/e7826499f3bc89d8070fe60129141632c6c9b609c2edcbbcba42c444f465dd63/68747470733a2f2f7472617669732d63692e6f72672f67656f72676568616e736f6e2f6c61726176656c2d706572736973746572732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/georgehanson/laravel-persisters) [![Coverage](https://camo.githubusercontent.com/9774dd92475b676bddc6f73e01514711ef60911473a4e3764f368ae552e7aa66/68747470733a2f2f636f6465636f762e696f2f67682f67656f72676568616e736f6e2f6c61726176656c2d706572736973746572732f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/georgehanson/laravel-persisters)

This package is designed to make it easy to create a persister class for your laravel project. The idea of persisters is to abstract the data storing process.

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

[](#installation)

To install the package, simply add the following to your composer.json file:

```
require: {
    ...
    "georgehanson/laravel-persisters": "^1.0"
    ...
}
```

Usage
-----

[](#usage)

### Creating Persisters

[](#creating-persisters)

To create a persister you simply need to create a new class, have it extend the base persister class and implement the abstracted methods. Here is an example:

```
use GeorgeHanson\LaravelPersisters\BasePersister;

class MyPersister extends BasePersister
{
    /**
     * Create a new Model
     *
     * @param array $data
     * @return Model
     */
    protected function create(array $data)
    {
        // Store a new resource here
    }

    /**
     * Update the given Model
     *
     * @param array $data
     * @param Model $model
     * @return Model
     */
    protected function update(array $data, Model $model)
    {
        // Update the given model here
    }
}
```

In order to use your new persister, you can simply instantiate the class and call the persist method. Below is an example:

```
$data = [
    'first_name' => 'John',
    'last_name' => 'Doe'
];

$persister = new MyPersister();

$persister->persist($data);
```

The base persister class will automatically work out whether you are creating a record or updating a record.

### Creating Records

[](#creating-records)

To create a new record using your persister class, simply call the persist method and pass in the data you which to save. The persist method can either accept an array of data, or a class which is `Arrayable` (such as a Collection, Request). Here is an example of creating a resource from a request.

```
use Illuminate\Http\Request;
use App\Persisters\MyPersister;

class MyController extends Controller
{
    public function store(Request $request, MyPersister $persister)
    {
        $record = $persister->persist($request);
    }
}
```

This will the fire the `create` method within your persister class you have created. Here you can handle any logic you wish for creating the resource.

### Updating Records

[](#updating-records)

Updating records is just as simple as creating records. The only difference is you have to pass a second parameter to the `persist` method which is the model you want to update. Below is an example of how you would update a record from a request.

```
use Illuminate\Http\Request;
use App\Persisters\MyPersister;
use App\User;

class MyController extends Controller
{
    public function update($id, Request $request, MyPersister $persister)
    {
        $user = User::find($id);

        // Update the user with the given data
        $record = $persister->persist($request, $user);
    }
}
```

### Filtering Data

[](#filtering-data)

We cannot be certain that the data we receive in our request is always the data we want to persist. For example, when we are saving the record we do not want to store the `_token` which is passed by Laravel for CSRF protection. We can do this simply by specifying the keys in the persister. This will then filter the data which has been passed and only return the data where that key exists. If you have specified a key in the keys array, however it is not found in the data being passed to the persister then it will set the value of that key to `null`. Here is an example of filtering the data:

```
use GeorgeHanson\LaravelPersisters\BasePersister;

class MyPersister extends BasePersister
{
    /**
     * The data to filter
     *
     * @type array
     */
    public $keys = [
        "first_name",
        "last_name"
    ];

    /**
     * Create a new Model
     *
     * @param array $data
     * @return Model
     */
    protected function create(array $data)
    {
        // No matter what is passed to us, $data will only contain "first_name" and "last_name"
    }

    /**
     * Update the given Model
     *
     * @param array $data
     * @param Model $model
     * @return Model
     */
    protected function update(array $data, Model $model)
    {
        // Update the given model here
    }
}
```

Alternatively, if you don't specify any keys it will return all of the data as an array.

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~201 days

Total

3

Last Release

2727d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/47cb0d219c881dd2b8f034738ae76fe3f82c1cd672ad60ff0f14c6be89a1662c?d=identicon)[GeorgeHanson](/maintainers/GeorgeHanson)

---

Top Contributors

[![georgehanson](https://avatars.githubusercontent.com/u/23167178?v=4)](https://github.com/georgehanson "georgehanson (3 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/georgehanson-laravel-persisters/health.svg)

```
[![Health](https://phpackages.com/badges/georgehanson-laravel-persisters/health.svg)](https://phpackages.com/packages/georgehanson-laravel-persisters)
```

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[orchestra/canvas

Code Generators for Laravel Applications and Packages

21017.2M158](/packages/orchestra-canvas)[illuminate/pipeline

The Illuminate Pipeline package.

9446.6M213](/packages/illuminate-pipeline)[illuminate/pagination

The Illuminate Pagination package.

10532.5M862](/packages/illuminate-pagination)[spatie/laravel-pjax

A pjax middleware for Laravel 5

513371.8k11](/packages/spatie-laravel-pjax)[spatie/laravel-mix-preload

Add preload and prefetch links based your Mix manifest

169176.0k2](/packages/spatie-laravel-mix-preload)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
