laranail/db-tools
Laranail DB Tools bundles practical database utilities for Laravel, offering convenient Artisan commands and helpers to inspect schemas, manage connections, and handle routine maintenance. A handy toolkit for debugging queries and keeping your database layer tidy.
Installation
composer require laranail/db-tools
Publish the config (if needed):
php artisan vendor:publish --provider="Laranail\DbTools\DbToolsServiceProvider"
Key Features to Explore
HasUlid or HasUuid traits in your models.
use Laranail\DbTools\Traits\HasUlid;
class Post extends Model
{
use HasUlid;
}
Schema::macro('addSoftArchive', function (Blueprint $table) {
// Custom logic
});
use Laranail\DbTools\Traits\SoftArchivable;
class Post extends Model
{
use SoftArchivable;
}
encrypted, json, or ulid.
protected $casts = [
'metadata' => 'encrypted',
'tags' => 'json',
'id' => 'ulid',
];
First Use Case Create a model with ULID and soft-archive:
php artisan make:model Post -t=HasUlid,SoftArchivable
Then run migrations to add the required columns.
UUID/ULID Integration
HasUlid or HasUuid in models requiring unique identifiers.bootHasUlid() or bootHasUuid() for custom logic (e.g., generating IDs on creation).protected static function bootHasUlid()
{
parent::bootHasUlid();
static::creating(function ($model) {
$model->{$model->getKeyName()} = Ulid::generate();
});
}
Schema Macros
Schema::macro('addTimestampsWithAudit', function (Blueprint $table) {
$table->timestamps();
$table->timestamp('audited_at')->nullable();
});
Schema::create('posts', function (Blueprint $table) {
$table->addTimestampsWithAudit();
});
Soft-Archive
$activePosts = Post::active()->get();
Post::where('is_published', false)->archive();
$post->restore();
Database Backups
php artisan db:backup --database=mysql --path=/backups
app/Console/Kernel.php:
$schedule->command('db:backup')->daily();
php artisan db:restore --file=/backups/backup_20260712.sql
Custom Casts
app/Providers/AppServiceProvider:
use Laranail\DbTools\Casts\CustomCast;
$this->app->bind('custom_cast', function () {
return new CustomCast();
});
protected $casts = [
'custom_field' => CustomCast::class,
];
UUID/ULID Conflicts
ulid trait requires binary(16) for ULID).HasUuid, confirm the column is char(36) or binary(16).php artisan tinker to test ID generation:
use Laranail\DbTools\Traits\HasUlid;
class TestModel extends Model { use HasUlid; }
$model = new TestModel();
$model->id; // Should output a ULID
Soft-Archive Scope Issues
active() scope excludes archived records by default. If you need to include them, use withArchived():
$allPosts = Post::withArchived()->get();
archived_at column exists in your migration. Missing it will cause errors.Schema Macro Overrides
Schema::macro('myapp_addIndex', function (Blueprint $table, $column) {
$table->index($column);
});
AppServiceProvider:
Schema::clearMacros();
Backup/Restore Quirks
--database flag matches your .env configuration.--skip-foreign-keys).max_execution_time in php.ini).--compress for large backups to save space:
php artisan db:backup --compress
Casts and Serialization
$post->toArray(); // Check if custom casts serialize correctly
Jsonable interface for complex casts:
use JsonSerializable;
class CustomCast implements JsonSerializable {
public function jsonSerialize() { ... }
}
Migration Conflicts
Schema::table('posts', function (Blueprint $table) {
$table->binary('id')->change(); // For ULID
});
Schema::hasColumn() to check for existing columns before modifying:
if (Schema::hasColumn('posts', 'archived_at')) {
$table->timestamp('archived_at')->nullable()->change();
}
Customizing ULID/UUID Generation
generateId() method in your model:
protected function generateId()
{
return Str::orderedUuid();
}
Adding Soft-Archive Events
SoftArchivable trait to dispatch events:
protected static function bootSoftArchivable()
{
parent::bootSoftArchivable();
static::archived(function ($model) {
event(new PostArchived($model));
});
}
Extending Backup Logic
php artisan make:command CustomBackupCommand
Then extend Laranail\DbTools\Console\Commands\BackupCommand.Adding Schema Macros for Teams
// app/Providers/SchemaMacrosServiceProvider.php
Schema::macro('addTeamColumns', function (Blueprint $table) {
$table->foreignId('team_id')->constrained();
$table->timestamp('team_joined_at')->useCurrent();
});
How can I help you explore Laravel packages today?