PHPackages                             simone-bianco/laravel-patches - 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. simone-bianco/laravel-patches

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

simone-bianco/laravel-patches
=============================

Laravel package to manage patches for your application, like database migrations but for code.

1.0.0(7mo ago)20MITPHPPHP &gt;=8.3

Since Sep 27Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/simone-bianco/laravel-patches)[ Packagist](https://packagist.org/packages/simone-bianco/laravel-patches)[ RSS](/packages/simone-bianco-laravel-patches/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (7)Versions (7)Used By (0)

Laravel Data Patches
====================

[](#laravel-data-patches)

[](https://www.google.com/search?q=https://packagist.org/packages/simonebianco/laravel-patches)[](https://www.google.com/search?q=https://packagist.org/packages/simonebianco/laravel-patches)[](https://www.google.com/search?q=https://github.com/simonebianco/laravel-patches/actions)

This package provides a robust, migration-like system for managing incremental data changes in your Laravel application. Instead of messy, non-repeatable seeders, you can create timestamped "patch" files that are tracked in the database, can be rolled back, and can be organized into subdirectories.

It's the perfect solution for:

- Seeding initial application data (like user roles, settings, countries, etc.).
- Deploying data changes to a production environment in a controlled and reversible way.
- Organizing complex data modifications into logical, ordered steps.

---

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

[](#installation)

You can install the package via composer:

```
composer require simonebianco/laravel-patches
```

Next, you should publish the package's assets (configuration file and migration):

```
php artisan vendor:publish --provider="SimoneBianco\Patches\PatchesServiceProvider"
```

This will publish:

- A configuration file to `config/patches.php`.
- A migration file to `database/migrations/`.

Finally, run the migration to create the `data_patches` table, which will track the executed patches.

```
php artisan migrate
```

---

Configuration ⚙️
----------------

[](#configuration-️)

The configuration file `config/patches.php` allows you to define global hooks that run before and after the patch processes. This is useful for tasks like disabling model observers during execution or clearing the application cache afterward.

Each hook must be a fully qualified class name that contains an `__invoke` method.

```
// config/patches.php

return [
    'callbacks' => [
        'up' => [
            // Executed before `patch:apply` starts
            'before' => null,
            // Executed after `patch:apply` finishes
            'after' => App\Patches\Hooks\ClearCache::class,
        ],
        'down' => [
            // Executed before `patch:rollback` starts
            'before' => null,
            // Executed after `patch:rollback` finishes
            'after' => null,
        ],
    ],
];
```

#### Example Hook Class

[](#example-hook-class)

```
// app/Patches/Hooks/ClearCache.php

namespace App\Patches\Hooks;

use Illuminate\Support\Facades\Artisan;

class ClearCache
{
    public function __invoke(): void
    {
        Artisan::call('cache:clear');
        Artisan::call('config:clear');
    }
}
```

---

Directory structure
-------------------

[](#directory-structure)

Below is an example structure for organizing your patches. You can nest as deep as you need:

[![Directory structure](z-doc/directories.png)](z-doc/directories.png)

---

Usage
-----

[](#usage)

### 1. Creating a Patch

[](#1-creating-a-patch)

To create a new patch, use the `make:patch` Artisan command. The patch files will be stored in the `database/patches` directory.

```
php artisan make:patch seed_initial_roles_and_permissions
```

This will create a file with the naming convention `YYYY_MM_DD_XXXXXX_seed_initial_roles_and_permissions.php`.

You can also create patches in subdirectories for better organization:

```
php artisan make:patch settings/site/add_maintenance_mode_setting
```

This will create the file inside `database/patches/settings/site/`.

### 2. The Patch File Structure

[](#2-the-patch-file-structure)

Each patch file is a simple class with two methods: `up()` and `down()`.

- `up()`: Contains the logic to apply the data change.
- `down()`: Contains the logic to reverse the change.

```
