0

I am working on a school project with multiyear database built in laravel. My requirement is to feed data for every new academic year in some tables. I have a main (superadmin) db and separate school's db for every school. I need to connect to school db, n process is i have kept superadmin db details in .env file then it fetches particular school's db details and makes a connection to that school db through middleware.

My question is when i execute migration and seeding command it connects to superadmin db and performs respective operation. But i want to execute migration/seeding one by one for every school's db.

James Z
  • 12,209
  • 10
  • 24
  • 44
charu
  • 11
  • 1
  • 4
  • 1
    Possible duplicate of [laravel multiple databases with multiple migration tables](https://stackoverflow.com/questions/42503780/laravel-multiple-databases-with-multiple-migration-tables) – Hamza Mohamed Mar 28 '18 at 06:52
  • @HamzaMohamed in that case he knows second database's credential but in my case those credentials are fetched from first db. I don't know 2nd db details, it comes from 1st db. – charu Mar 28 '18 at 07:00

2 Answers2

1

Add a extra entry into config/database.php For example mysql2

On each of your models specify the database it is related to. For example on your School Model

protected $connection = 'mysql2';

Now you can just run your seeder like you have 1 database , since the connection is specified on your models the correct database will be seeded.

Christophvh
  • 12,586
  • 7
  • 48
  • 70
1

you can run the command

php:artisan db:seed --class="className" --database=mysql2
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
rupesh-mdr
  • 71
  • 11
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jul 23 '22 at 02:07