Laravel: Run migrations on another database
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Laravel: Run Migrations on Another Database for User-Specific Databases
In this blog post, we will explore the possibility of running migrations on a newly created user's database within your Laravel application. The process involves creating a separate database for each registered user and managing their respective databases through migration scripts.
1. Creating a User Model with Database Connection Information (app/User.php)
First, we need to create a User model that contains the necessary data to establish a connection with its corresponding database. This would include attributes such as 'database_name', 'username', and 'password'. In addition, you may also store additional information related to the user, such as their ID or any specific preferences for database management.
Example:
$this->database_name,
'username' => $this->username,
'password' => $this->password,
]);
}
}
2. Creating a New Website Model and Database (app/Models/Website.php)
Next, we need to create a Website model that will maintain information about each website created by a user. This model should contain the necessary attributes and methods for managing websites, including the foreign key relationship with the User model.
Example:
belongsTo(User::class);
}
}
3. Establishing the Connection and Running Migrations on User's Database
To run migrations on a new user's database, we need to create a method in our Website model that will establish the connection and perform any necessary actions. In this example, we will also include a transaction to ensure consistency when executing multiple queries simultaneously.
Example:
user_id = Auth::get()->id;
$website->save();
$database_name = 'website_' . $website->id;
DB::statement(DB::raw('CREATE DATABASE ' . $database_name));
// Establish the connection to the user's database and run migrations
$userDatabaseConnection = $this->getDatabaseConnection($website);
// Run any necessary migration scripts on the new user's database
(new \Illuminate\Database\Migrations\Migrator($userDatabaseConnection))->run();
});
} catch(\Exception $e) {
echo $e->getMessage();
}
}
}
4. Handling Exceptions and Displaying Errors on Failure
In the event that an exception occurs during the process, we should display the error message to alert users about any issues encountered while creating their databases or running migrations.
5. Conclusion
By following these steps and incorporating proper Laravel practices, you can ensure a seamless experience for your users when managing their own databases. The ability to run migrations on user-specific databases offers greater flexibility and control over their application data, ultimately enhancing their overall user experience.