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.
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
php artisan make:update update_name
return new class
{
/**
* Run the updates.
*/
public function __invoke(): void
{
//
}
};
php artisan db:update
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', '<', '2023-01-01')
->delete();
User::query()
->where('created_at', '<', '2023-01-01')
->delete();
}
You have made some refactoring and you also renamed a state name, so you need to rename it in the database:
/**
* Run the updates.
*/
public function __invoke(): void
{
Order::query()
->where('state', 'new')
->update(['state' => OrderState::CREATED]);
}
composer test
Please see CHANGELOG for more information on what has changed recently.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.