Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jimb814's avatar

How to use API to insert data from one application into the DB of another application

I am new to Laravel. I am attempting to pass data from one application (App 1) to the database of another application (App 2). I am dizzy from the tutorials and articles I have studied on APIs and Laravel and Guzzle. I have managed to patch something together to create a new record, but it is inserted into the wrong database: App 1's DB instead of App 2's.

Route in web.php in App 1:

Route::get('post-registrant', [DataController::class, 'postRequest']);

postRequest() in DataController

public function postRequest(Request $request) 
    {
        $client = new \GuzzleHttp\Client();
        $url = 'http://thepipeline.localhost/api/v1/registrants/store';

        $form_params = [
            'email'             => '[email protected]',
            'first_name'    => 'John',
            'last_name'     => 'Smith',
              /. . . .
        ];

        $response = $client->post($url, ['form_params' => $form_params]);
        $response = $response->getBody()->getContents();
}

The $url value above is the route to the API in App 2.

http://thepipeline.localhost/api/v1/registrants/store

App 2 route in api.php

Route::post('v1/registrants/store', [RegistrantsController::class, 'store']);

store() in RegistrantsController in App 2

public function store(Request $request)
{
        $registrant = Registrant::create($request->all());
        return response()->json('Successfully added');
}

On my local machine, I have a DB for App 1 and a DB for App 2. The table I want to add the data into is App2.registrants but the data is being inserted into App1.registrants

If anyone can help identify what is wrong, if not everything, I'd appreciate it.

0 likes
3 replies
bugsysha's avatar

What error appears? Is there anything in the log files?

If App1 can connect to the database of the App2 then you can just add new model, specify that it should use that second database and save it. If not then you have to create a route on the App2 and send data via some HTTP client. Newer Laravel versions have Http facade that you can use as your HTTP client to send the data.

jimb814's avatar

Thank you @bugsysha for your reply. There is no error because the code is successfully inserting data, but into the "wrong" DB. It doesn't make sense since the Controller doing the work is in another application (App2).

I thought using the Registrant model--Registrant::create($request->all());--means only that the data will be inserted into the Registrants table of App2 where the code is executed.

As far as specifying a second DB, are you suggesting something like this?

DB::table('registrants')->insert(
    [ array of key value pairs ]
);

Instead of this:

Registrant::create($request->all());

The URL in the postRequest() method in App1

$url = 'http://thepipeline.localhost/api/v1/registrants/store';

is an endpoint/route in App2. What new route in App2 to send data via some HTTP client are you referring?

The store() method in App2

public function store(Request $request)
{  
        $registrant = Registrant::create($request->all());
        return response()->json('Successfully added');
}

creates the new record, so how is it possible that it stores it in another DB?

I tested the API in Insomnia and it works as expected. I'm unsure if headers are required. I don't know how to send those or other critical data such as authorization, and other info the server might need. More fun is waiting for me.

I cannot rule out the idea that I am missing this entirely. Clarity doesn't seem possible except by way of confusion.

jimb814's avatar

I resolved the issue. I am running XAMPP in Windows.

Solution: C:\Windows\System32\drivers\etc\hosts was modified where the value of 127.0.0.1 was set to App1.

Explanation: My server is setup to allow opening several projects from htdocs with the extension .localhost. I did not know that using php artisan server that you can open multiple apps by assigning different ports. In attempting to solve another issue a few days ago, I read somewhere that it was resolved by making this assignment. Because I made this change a few days ago and what was happening made no sense, I suspected this might be it. So using php artisan serve instead of Apache Server, I started App1 as 127.0.0.1:8000 and App2 as 127.0.0.1:8001.

I ran the code again and the data was inserted into the correct DB, App2's DB.

The code at the bottom of this file was changed like this, over-riding the code:

##################################

127.0.0.1	thepipeline.localhost

##################################

I changed it back by commenting out the line:

##################################

#127.0.0.1	thepipeline.localhost

##################################

Thanks to the 20+ folks in this community who read my explanation in the hopes of lending a helping hand.

Please or to participate in this conversation.