Installation
composer require antimattr/mongodb-migrations
Add the service provider to config/app.php:
'providers' => [
// ...
Antimattr\MongoDBMigrations\MongoDBMigrationsServiceProvider::class,
],
Publish Configuration
php artisan vendor:publish --provider="Antimattr\MongoDBMigrations\MongoDBMigrationsServiceProvider" --tag="config"
Edit config/mongodb-migrations.php to match your MongoDB connection (e.g., mongodb in config/database.php).
First Migration Create a migration file:
php artisan make:migration create_users_collection --collection=users
Define schema in database/migrations/[timestamp]_create_users_collection.php:
use Antimattr\MongoDBMigrations\Migration;
use Antimattr\MongoDBMigrations\Schema\Collection;
class CreateUsersCollection extends Migration
{
public function up()
{
Collection::create('users', function ($collection) {
$collection->index('email', ['unique' => true]);
$collection->index('created_at');
});
}
public function down()
{
Collection::drop('users');
}
}
Run Migrations
php artisan mongodb:migrate
up() method to modify the schema:
$collection->field('status', ['type' => 'string', 'default' => 'active']);
php artisan mongodb:migrate
Version Control
database/migrations/ alongside Eloquent migrations.php artisan mongodb:migrate --step=1 for incremental testing.Seeding Data
Combine with laravel/breeze or seeds:
public function up()
{
Collection::create('users', function ($collection) {
// Schema...
});
// Seed after creation
DB::collection('users')->insert([...]);
}
Rollbacks
php artisan mongodb:migrate:rollback or define down() in migrations.public function down()
{
DB::transaction(function () {
Collection::drop('users');
// Additional cleanup...
});
}
$collection->index('email', ['unique' => true, 'sparse' => true]);
$collection->field('metadata', ['type' => 'object']);
$collection->field('tags', ['type' => 'array']);
$collection->field('age', [
'type' => 'int',
'min' => 18,
'max' => 120,
]);
Event Listeners
Trigger migrations on registered or booted:
public function boot()
{
if ($this->app->environment('production')) {
Artisan::call('mongodb:migrate');
}
}
Environment-Specific Migrations
Use --env=testing flag or conditional logic:
public function up()
{
if (app()->environment('local')) {
Collection::create('logs', [...]);
}
}
Schema Drift
down() to revert changes carefully:
public function down()
{
// Drop indexes first if needed
DB::collection('users')->dropIndex('email');
Collection::drop('users');
}
Transaction Limitations
retryableWrites: true in connection config.php artisan mongodb:migrate:rollback --step=1
Index Conflicts
$collection->index('email_1', ['unique' => true]); // Suffix with _1 if needed
Dry Runs
Use --pretend to preview changes:
php artisan mongodb:migrate --pretend
Logging
Enable debug mode in config/mongodb-migrations.php:
'debug' => env('APP_DEBUG', false),
Check logs for failed migrations in storage/logs/laravel.log.
MongoDB Shell Verification Manually verify schema changes:
mongo --eval 'db.users.getIndexes()'
Custom Commands Extend the base command:
php artisan make:command CustomMongoMigrate
Override handle() to add logic (e.g., pre-migration checks).
Migration Events
Listen for migrating or migrated events:
Event::listen('migrating', function () {
Log::info('Starting MongoDB migrations...');
});
Custom Schema Builders
Extend Antimattr\MongoDBMigrations\Schema\Builder for reusable logic:
class CustomBuilder extends Builder
{
public function addAuditFields()
{
$this->field('created_at', ['type' => 'date']);
$this->field('updated_at', ['type' => 'date']);
}
}
Use in migrations:
$collection = new CustomBuilder($collectionName);
$collection->addAuditFields();
Connection Binding
Ensure config/database.php has the correct MongoDB connection:
'connections' => [
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', '127.0.0.1'),
// ...
],
],
Migration Table
The package uses migrations collection by default. Customize in config:
'migrations_table' => 'custom_migrations',
Timeouts Increase timeout for large migrations:
'timeout' => 300, // 5 minutes
How can I help you explore Laravel packages today?