Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

A package to easily backup database tables in Laravel

Sometimes what happens is that for some reason, you might need to back up your database tables.

For instance, you might want to restore them in a different environment or take a backup of your database tables before performing some migrations.

It might be considered a good practice to back up your tables before doing anything destructive. Anything can go haywire, you never know.

This is where this Laravel package that I stumbled upon recently can come in handy.

Essentially, this package lets you back up a table or a set of tables from within your code. And that’s pretty much it. I love such single-purpose packages!

First, install the package using Composer.

composer require watheqalshowaiter/backup-tables

Once installed, you can use the BackupTables facade to back up a table or a set of tables.

So, for instance, if you want to backup the users table, you can do it like so.

use WatheqAlshowaiter\BackupTables\BackupTables;

Route::get('/backup', function () {
    BackupTables::generateBackup(['users', 'limits']);
});

This will generate a backup/snapshot of the users table in the database with the format users_backup_2024_09_03_13_58_31 where it appends the current date and time to the table name.

You can also back up multiple tables at once.

BackupTables::generateBackup(['users', 'posts']);

You can change the naming convention of the generated table by passing a second argument to the generateBackup method.

BackupTables::generateBackup(['users', 'posts'], 'Y_d_m');

This will generate the table name as users_backup_2024_09_03. But do know that it will skip the backing up of the tables if the table is generated with the same name.

And that’s about it. This package lets you do what it’s advertised to do but what I would really like is a command-driven approach to back up your tables as well.

I suppose it will be there in the future version of this package.

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a

Tip

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?