charu
charu

Reputation: 11

Seeding in laravel for multiple databases

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.

Upvotes: 0

Views: 1658

Answers (2)

rupesh-mdr
rupesh-mdr

Reputation: 71

you can run the command

php:artisan db:seed --class="className" --database=mysql2

Upvotes: 1

Christophvh
Christophvh

Reputation: 13254

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.

Upvotes: 1

Related Questions