Laravel API Connecting to Client Database

ckaryuusai

I'm building 2 websites, one of them will be hosted on the server and the other one on localhost, both have their own database. The one on the server has API routes getting data from client and insert it to database. The function i'm trying to implement is that the one on localhost will be retrieving data from its own database, then call the API route of the other one on the server then inserting it to the server's database.

expected result : Data from localhost database inserted into the server's database via API.

actual result : The server is inserting the data back into the localhost database.

question : Is there a way to force the server to insert the data to it's own database?

codes : Localhost function

public function Sync (){
    $payload = t_worksheet::all()->toArray();
    $client = new Client();
    $res = $client->request('POST', 'http://localhost/cal-work/public/api/sync', ['form_params' => 
    ['payload' => $payload]]);
}

Server function

public function sync (Request $request) {
    $payload = $request->payload;
    foreach ($payload as $item) {
        $target = new t_worksheet_server;
        $target->name = $item['name'];
        $target->details = $item['details'];
        $target->approved_by = $item['approved_by'];
        $target->save();
    }

    return response()->json('success', 200);
}

Server API routes

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::post('/sync', 'SyncController@sync');

Local .env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=ws-local
DB_USERNAME=root
DB_PASSWORD=12345678

Server .env

DB_CONNECTION=mysql
DB_HOST=db4free.net
DB_PORT=33060
DB_DATABASE=ws-server
DB_USERNAME=ckaryuusai
DB_PASSWORD=12345678

Thanks in advance

ckaryuusai

It worked, i served the server project with php artisan serve --port=8080 and changed the guzzle client to localhost:8080, it worked fine

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Laravel 4 not connecting to database

Connecting API to database

Connecting API with laravel project

connecting to database issue in laravel 5.1

Client having issues connecting to Azure SQL Database

Google Client Api is not connecting on activity resume

Connecting fail when connect to the Google Api Client

Laravel application on the AWS ElasticBeanstalk is not connecting to the RDS database

Laravel 5.1 - Connecting to MySQL Database (MAMP)

Connecting database fields and model attributes in Laravel

Loopback Rest API without connecting to database

Connecting AzureDatabricks on a CosmosDB MongoDB API database

Connecting the neo4j web client to a local database

sync client database data to master database - laravel

Laravel compare database and api

database insertion in Laravel API

Strange behaviour with Android Activity Lifecycle when connecting to Google API Client

Google API client not connecting again after onConnectionSuspended(int Cause) called

Uncaught Autodesk\Forge\Client\ApiException: [504] Error connecting to the API phpSDK

Laravel is not connecting to database on Docker/ docker-compose on Windows

Connecting a db2 database from a centos hosted laravel application

Trouble connecting to my database with entity framework c# web api

ASP. Net Web API service connecting to oracle database

Webservice connecting do database with IOS, PHP, REST API, JSON

Error connecting stripe api on laravel project Stripe\Exception\ApiConnectionException

Docker: Level of separation between database, client and API

ignite client connecting to the server

SignalR client not connecting

telnet client not connecting -- to QTCPserver