PHPackages                             rockero-cz/laravel-db-updates - 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. rockero-cz/laravel-db-updates

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

rockero-cz/laravel-db-updates
=============================

This is my package laravel-db-updates

1.0.2(2y ago)83.5k↓50%[2 PRs](https://github.com/rockero-cz/laravel-db-updates/pulls)MITPHPPHP ^8.1

Since Oct 28Pushed 2y agoCompare

[ Source](https://github.com/rockero-cz/laravel-db-updates)[ Packagist](https://packagist.org/packages/rockero-cz/laravel-db-updates)[ Docs](https://github.com/rockero-cz/laravel-db-updates)[ RSS](/packages/rockero-cz-laravel-db-updates/feed)WikiDiscussions main Synced 1mo ago

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

  ![Banner](https://camo.githubusercontent.com/fd443f22d0c43e559e0bba2ded60261b659dd5cd27ce89dd6eaab1cb2ea011c5/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c2532304442253230557064617465732e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d726f636b65726f2d637a2532466c61726176656c2d64622d75706461746573267061747465726e3d67726170685061706572267374796c653d7374796c655f31266465736372697074696f6e3d4669782b6f722b7570646174652b796f75722b646174612b656173696c792b776974682b64617461626173652b757064617465732e266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313030707826696d616765733d6461746162617365)Laravel DB Updates
==================

[](#laravel-db-updates)

[![Rockero](https://camo.githubusercontent.com/4d4c49ffa87da81dc07dfe374ac2cee7a9ad9c98ec58482f144eab60338fbe21/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f526f636b65726f2d79656c6c6f77)](https://rockero.cz)[![Latest Version on Packagist](https://camo.githubusercontent.com/f9881dace9ee550602a6b06955f00212632f92330cb8a782a231c80e6a4e60c6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f726f636b65726f2d637a2f6c61726176656c2d64622d757064617465732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/rockero-cz/laravel-db-updates)[![Total Downloads](https://camo.githubusercontent.com/496736f9ee2b5dfa363e7580f89b46b6ea71e13d23a78d75977311239e0546f6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f636b65726f2d637a2f6c61726176656c2d64622d757064617465732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/rockero-cz/laravel-db-updates)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE)

In short, database updates package is used to update or fix your data in database. It works on the same principle as Laravel migrations.

Each `DB update` can be run only once across environments.

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

[](#installation)

Install the package via composer:

```
composer require rockero-cz/laravel-db-updates
```

Publish and run migrations with:

```
php artisan vendor:publish --tag="db-updates-migrations"
php artisan migrate
```

Generating Updates
------------------

[](#generating-updates)

```
php artisan make:update update_name
```

```
return new class
{
    /**
     * Run the updates.
     */
    public function __invoke(): void
    {
        //
    }
};
```

Running Updates
---------------

[](#running-updates)

```
php artisan db:update
```

Usage Examples
--------------

[](#usage-examples)

**Below you can find some practical examples.**

Until now, a post could only have one image, now it can have multiple ones though, so you need to transfer your post image to the separate `images` table:

```
/**
 * Run the updates.
 */
public function __invoke(): void
{
    Post::all()->each(function (Post $post) {
        $post->images()->create([
            'url' => $post->image,
            'title' => $post->title
        ]);
    });
}
```

Your production database had some testing data and you finally decided to delete them, so you need to delete all records older than `2023-01-01`:

```
/**
 * Run the updates.
 */
public function __invoke(): void
{
    Post::query()
        ->where('created_at', '
