Skip to content
Advertisement

Laravel uses incorrect database when Http request is sent from another Laravel app

I seem to be running into a weird issue. I am triggering a post request using the Http facade from a laravel app to another laravel app. Both these apps are linked to separate databases. When I try to trigger the same endpoint using postman, it works fine but when the request is triggered from the other laravel app, the recipient laravel app tries to use the sender app’s database settings which doesn’t work. I am currently using Xampp on Windows to host both these apps and the packages are the latest versions. Has anyone experinced a similar issue or could you suggest a solution?

The code is as follows: The service which sends the POST request (Sender App (1)):

Http::post("http://localhost/second_app/public/api/test", array(
                    'id' => 1,
);

The code which received the request (Receiver App (2)):

public function test(Request $request)
{
    $club = Club::find($request->id);
}

I get an error in the log file which says that it is trying to find the clubs table in the first_app database while it should be using the second_app database. I tried logging the configurations and the request. The request is quite large to be posted here but I verified that is received correctly. The code for this log is:

Log::info("Received Request", ['database' => ['driver' => config('database.default'), 'name' => config('database.connections.'.config('database.default').'.database')]]);

If the request is sent from Postman to second_app or from the second_app to itself (using the Http facade)

[2021-08-17 03:13:56] local.INFO: Received Request {"database":{"driver":"mysql","name":"second_app"}} 

If the request is send from first_app to second_app using the Http facade

[2021-08-17 03:14:01] local.INFO: Received Request {"database":{"driver":"mysql","name":"first_app"}} 
[2021-08-17 03:14:01] local.INFO: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'first_app.projects' doesn't exist (SQL: select * from `projects` where `code` = ABC_01 limit 1) {"exception":"[object] (Illuminate\Database\QueryException(code: 42S02): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'first_app.projects' doesn't exist (SQL: select * from `projects` where `code` = ABC_01 limit 1) at \vendor\laravel\framework\src\Illuminate\Database\Connection.php:692)

Config for the apps is left the same as default. The .env is filled with the following details

#first_app
DB_DATABASE="first_app"

#second_app
DB_DATABASE="second_app"

UPDATE I tried with separate vhosts as well. firstapp.test and secondapp.test was set up and the document root was pointed to the public directories. The issue remained the same and the incorrect configuration was used when the request was sent from first_app but it works correctly (as it did earlier) when the request is sent internally from the second_app or from Postman

Advertisement

Answer

I tried hard-coding the database.php configuration values in the recipient app instead of using the env helper and that fixed the issue. It looks like the env helper was causing a conflict by using the sender app’s environment variables in the recipient app.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement