如何通过laravel为不同的客户使用不同的数据库?

I develop a software to plan and manage meetings in Schools. Everything is working fine so far but now I am at the point where I need to serve my laravel application to multiple schools without using the same database for security reasons.

Now I want to use different MySQL-Connections depending on which url the client comes from.

For example:

  • school1.example.org uses DB1,
  • school2.example.org uses DB2 and so on.

My backend should only be on one server with external Database servers. So maybe something like this:

  • API Server: 99.99.99.10
  • DB1 Server: 88.88.88.10
  • DB2 Server: 88.88.88.20
  • UI Server: 77.77.77.10

How could I possibly realize that? Ideally that will scale automatically if new schools want to use this, but if there is manual work needed for deployment that will be okay at the beginning.

I hope that you understand what I need and can help me with that.

Best regards, Marvin

You can follow this approach and improve according to your requirements.

  1. Master DB

    Create Schools table. In this, for each school, keep a record with the subdomain (unique identifier) and School DB connection information.

  2. School DBs

    This would contain all tables for your app. Each school will have a separate DB.

  3. Keep only the Master DB credentials in config/database.php.
  4. Using a ServiceProvider set the active School DB as the default database connection by querying the Master DB/ Schools Table using the subdomain.