Support the ongoing development of Laravel.io →
Database Eloquent

Hello, im having a logic problem, sorry if this is not the place to put this.

Im working on an app for an educational institution to send messages (email, sms, facebook, etc) to the students, or others users. So i have tables like students, registers, etc. Now is working fine, but is just a single institution. The problem is when exist more than one institution. I could have the app in each domain of the institution, but if, eventually i have 10+, it would be difficult to update, mantain, etc.

What i want to do is from a single login form (e. app.domain.com/login), the logged user , depends on what institution is related, connect to the specific database from that institution.

I dont know if this the best way to do this, or if i have to look in another direction.

Someone has told me about connection strings associated to each user but i dont know if this is a good practice.

Hope you can help me. Thanks.

P.D.: Sorry for my english.

Last updated 2 years ago.
0

Why not just create an institution table and add an institution_id foreign key in your other tables?

0

KevinCreel said:

Why not just create an institution table and add an institution_id foreign key in your other tables?

this will word. yes

you can also try writing a wrapper layer that automatically selects the working database based on the sub domain. (multi tenancy) there are many tutorials if you search by the correct keywords

0

I'd suggest doing multi-tenant application which has separate databases for each school.

You do NOT want to show student data to other schools or people. By having separate databases you keep your data separate!

0

I tried what Kevin and astroanu suggest, but i think is not the best way. Maybe im wrong...

illuminate3 said:

I'd suggest doing multi-tenant application which has separate databases for each school.

You do NOT want to show student data to other schools or people. By having separate databases you keep your data separate!

Thats what im trying to do now, but im not sure how i have to this. My approach is having a main DB with the users table, maybe connection table, roles, permissions...

I know that i can define relations across multiple databases, but in order to achieve what i want, how i define the relation , considering that could be in a different database? and where i should define the pivot table to the relations of users on the MAIN-DB and, for example, grades on DB-B?

###-------------EDIT---------------- what im doing now, is trying to stablish the relationship between users, and grades, but i dont know how to do this, because users is on the MAIN_DB, and grades is in the SCHOOL_DB, if i put the pivot table in MAIN, how i reference the FK, of the GRADE if the database changes?

For example in this case, i cannot simply do this:

public function up()
{
	Schema::create('usuario_grupo', function (Blueprint $table) {
        $table->integer('user_id')->unsigned();
        $table->integer('grupo_id')->unsigned();

        $table->foreign('user_id')->references('id')->on('users')
            ->onUpdate('cascade')->onDelete('cascade');

        */ I cannot refer the name of the DB of the school because this name change with every school /*
        $table->foreign('grupo_id')->references('id')->on('schoolA.grupos')
            ->onUpdate('cascade')->onDelete('cascade');

        $table->primary(['user_id', 'grupo_id']);
    });
}

Any suggestion of how i should do this?

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

rolinad rolinad Joined 6 Jul 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.