0

I have configured Laravel 5.2 to contain 2 database connections. I know how I can connect to each one within my controllers, models etc. The only thing I am unsure of is the following.

System A has a users table which contain all the users of system A.

I am now working within System B. System B has its own users table with all the users of System B. System B need to get a list of users in System A.

This is where the problem is. For system B I am creating my migration. System B allows for users of this system to create a project so my migration is as follows

Schema::create('projects', function (Blueprint $table) {
    $table->increments('id');
    $table->string('projectName')->default('');
    $table->string('projectValue')->default('');
    $table->integer('user_id')->unsigned()->default(0);
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    $table->integer('systemA_user_id')->unsigned()->default(0);
    $table->foreign('systemA_user_id')->references('id')->on('users')->onDelete('cascade');

    $table->engine = 'InnoDB';
});

By doing the following, I can link the project in System B to a User in System B.

$table->integer('user_id')->unsigned()->default(0);
  $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

However, although this project is created in System B, it is for a user in System A. Therefore I have added the following

$table->integer('systemA_user_id')->unsigned()->default(0);
    $table->foreign('systemA_user_id')->references('id')->on('users')->onDelete('cascade');

The problem is that it is currently referencing the System B users table. What I need it to do is reference the System A users table.

Would something like this be possible?

Thanks

katie hudson
  • 2,765
  • 13
  • 50
  • 93

2 Answers2

0

I believe you could do it this watreferences('connection.tablename.id')

Sari Yono
  • 577
  • 5
  • 13
0

It smells like a bad design. You have to consider establishing some sort of API connection between your systems instead of trying to have low level infrastructure to communicate directly. Microservices way of thinking.

dogik
  • 71
  • 1
  • 2
  • What would be the best option in this situation then? System B needs to get something from System A's database. Should I create an API for this? – katie hudson Aug 10 '16 at 17:47
  • If there is a point in separating the databases, then there got to be a reason for logically separating your systems and then connect them through API. Maybe even using different frameworks or even programming languages. – dogik Aug 11 '16 at 09:08
  • However, on the other hand, if you don't think that you have time/desire/good reason for separating your systems on code level or if that's a legacy separation of the databases, then I would try to union those databases to create one, monolith database. If you expect a lot of connections between the databases (read joins, subqueries etc) and taking into consideration that you are using laravel (where eloquent sometimes a hard ass even when you working with single database), I would recommend colliding the databases, if possible. – dogik Aug 11 '16 at 09:08
  • This does not provide an answer to the question. Once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/13295397) – lokusking Aug 14 '16 at 09:48
  • @lokusking yeah, I am sorry.. That answer should make people start thinking.. I have completely forgotten that stackoverflow is not suppose to do such horrible things to human beings. My bad! – dogik Oct 17 '16 at 18:14